我的table如下用户ID(主键)历史记录(整数)在history单元格中,我需要在integer-example类型的单个单元格中插入多个值-
1 history:(1,4,6)2 history:(2,7,9)
1 history:(1,4,6)
2 history:(2,7,9)
另外,我必须更新值,比如before->history(1,4,6)after->history(1,4,5,7)
zbsbpyhn1#
我需要在一个单元格中插入多个值,这是一个整数类型的例子你不能这么做。整数值就是:存储在给定列中的单个整数值。如果要将多个值与给定的 user_id ,则这是一对多关系。通常将其表示为一个单独的表,每个表一行 (user_id, history) 元组,就像这样:
user_id
(user_id, history)
user_id history1 11 41 62 22 72 9
user_id history
1 1
1 4
1 6
2 2
2 7
2 9
此表的主键将由两列的组合构成,其中 user_id 列作为 users table。然后,您可以使用聚合查询生成逗号分隔的列表(如果需要):
users
select user_id, group_concat(history) all_historyfrom user_historygroup by user_id
select user_id, group_concat(history) all_history
from user_history
group by user_id
你可以把原来的table和 join :
join
select u.*, uh.all_historyfrom users uinner join ( select user_id, group_concat(history) all_history from user_history group by user_id) uh on u.user_id = uh.user_id
select u.*, uh.all_history
from users u
inner join (
) uh on u.user_id = uh.user_id
ohtdti5x2#
听起来你想 group_concat() :
group_concat()
select userid, group_concat(history)from tgroup by userid;
select userid, group_concat(history)
from t
group by userid;
2条答案
按热度按时间zbsbpyhn1#
我需要在一个单元格中插入多个值,这是一个整数类型的例子
你不能这么做。整数值就是:存储在给定列中的单个整数值。
如果要将多个值与给定的
user_id
,则这是一对多关系。通常将其表示为一个单独的表,每个表一行(user_id, history)
元组,就像这样:此表的主键将由两列的组合构成,其中
user_id
列作为users
table。然后,您可以使用聚合查询生成逗号分隔的列表(如果需要):
你可以把原来的table和
join
:ohtdti5x2#
听起来你想
group_concat()
: