我有 2 个表,分别名为 vhistory 和 week2。 vhistory 包括两列 voternum 和 voterhistory。 week2 包含列 age、address、voternum、voterhistory、status、gender、precinct、county 和 zip5。我需要使用表 vhistory 更新 week2 voterhistory,其中 week2 voternum = vhistory voternum。
例如:
在第 2 周内,有一个人的 voternum = 1234。
在 vhistory 中有 3 条记录,其中 voternum = 1234。
一个 voterhistory = 2011,一个 voterhistory = 2012,一个 voterhistory = 2013。
这意味着当更新 week2 时,vhistory 的 3 条记录应该像这样导入到 week2 表的 voterhistory 列中:2011、2012、2013。
这就是我目前所拥有的。
UPDATE week2 SET voterhistory=SELECT CONCAT_WS(',',SELECT voterhistory FROM vhistory
WHERE week2.voternum = vhistory.voternum );
最佳答案
我认为您正在寻找更新连接语句:
UPDATE week2
JOIN (SELECT voternum,
GROUP_CONCAT(voterhistory ORDER BY voterhistory SEPARATOR ',')
AS history
FROM vhistory
GROUP BY voternum) h ON week2.voternum = h.voternum
SET voterhistory = history
关于mysql - 使用 CONCAT_WS 错误更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23299863/