mysql的选择更新 [英] mysql select update
问题描述
得到这个:
Table a
ID RelatedBs
1 NULL
2 NULL
Table b
AID ID
1 1
1 2
1 3
2 4
2 5
2 6
表需要在表B给出一个有一个逗号分隔的列表。然后表B将成为过时的:
Need Table a to have a comma separated list as given in table b. And then table b will become obsolete:
Table a
ID RelatedBs
1 1,2,3
2 4,5,6
这确实通过所有记录不RUND,只是广告中的一个B到表中的
This does not rund through all records, but just ad one 'b' to 'table a'
UPDATE a, b
SET relatedbs = CONCAT(relatedbs,',',b.id)
WHERE a.id = b.aid
更新:谢谢,3个正确答案(标记为最古老的答案)! GROUP_CONCAT是使用的一个。无需使用relatedids = CONCAT(relatedids,',',NEXT_ID)做到这一点通过GROUP_CONCAT自动插入ID之间的逗号。
UPDATE: Thanks, 3 correct answers (marked oldest as answer)! GROUP_CONCAT is the one to use. No need to insert commas between the ids using relatedids = CONCAT(relatedids,',',next_id) that is done automatic by GROUP_CONCAT.
推荐答案
您将不得不使用MySQL的GROUP_CONCAT功能,以实现这一目标:的http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_group-concat
You'll have to use the mysql group_concat function in order to achieve this: http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_group-concat
这篇关于mysql的选择更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!