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

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

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

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

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

zbsbpyhn

zbsbpyhn1#

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

user_id     history
1           1
1           4
1           6
2           2
2           7
2           9

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

select user_id, group_concat(history) all_history
from user_history
group by user_id

你可以把原来的table和 join :

select u.*, uh.all_history
from users u
inner join (
    select user_id, group_concat(history) all_history
    from user_history
    group by user_id
) uh on u.user_id = uh.user_id
ohtdti5x

ohtdti5x2#

听起来你想 group_concat() :

select userid, group_concat(history)
from t
group by userid;

相关问题