sql—如何在mysql中特定列的单个单元格中插入多个整数值

gj3fmq9x  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(334)

我的table如下
用户ID(主键)
历史记录(整数)
在history单元格中,我需要在integer-example类型的单个单元格中插入多个值-

  1. 1 history:(1,4,6)
  2. 2 history:(2,7,9)

另外,我必须更新值,比如before->history(1,4,6)after->history(1,4,5,7)

zbsbpyhn

zbsbpyhn1#

我需要在一个单元格中插入多个值,这是一个整数类型的例子
你不能这么做。整数值就是:存储在给定列中的单个整数值。
如果要将多个值与给定的 user_id ,则这是一对多关系。通常将其表示为一个单独的表,每个表一行 (user_id, history) 元组,就像这样:

  1. user_id history
  2. 1 1
  3. 1 4
  4. 1 6
  5. 2 2
  6. 2 7
  7. 2 9

此表的主键将由两列的组合构成,其中 user_id 列作为 users table。
然后,您可以使用聚合查询生成逗号分隔的列表(如果需要):

  1. select user_id, group_concat(history) all_history
  2. from user_history
  3. group by user_id

你可以把原来的table和 join :

  1. select u.*, uh.all_history
  2. from users u
  3. inner join (
  4. select user_id, group_concat(history) all_history
  5. from user_history
  6. group by user_id
  7. ) uh on u.user_id = uh.user_id
展开查看全部
ohtdti5x

ohtdti5x2#

听起来你想 group_concat() :

  1. select userid, group_concat(history)
  2. from t
  3. group by userid;

相关问题