問題描述
明白了:
Table a
ID RelatedBs
1 NULL
2 NULL
Table b
AID ID
1 1
1 2
1 3
2 4
2 5
2 6
需要表 a 具有表 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
這不會遍歷所有記錄,而只是將一個 'b' 添加到 'table a'
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 是要使用的.無需使用由 GROUP_CONCAT 自動完成的 relatedids = CONCAT(relatedids,',',next_id) 在 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 選擇更新的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!