mysql - 使用 CONCAT_WS 错误更新表

标签 mysql sql concatenation concat-ws

我有 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/

相关文章:

php - 有没有办法将 "obfuscatd"文本插入 MySql,这样我就无法读取我的用户存储在其中的内容?

MYSQL CASE 语句(if/else)

mysql - 具有having count 的Where 子句会破坏查询

php - MySQL - 如何插入可能为空的值

javascript - 如何在 JavaScript 中的 for 循环中创建动态数组元素名称?

python - Pandas 系列中列表的有效串联

sql - 使用Hive ODBC连接器时, Pandas read_sql()不喜欢DISTINCT关键字

php - 如何将变量传递给 codeigniter 中的条件连接?

sql - 查找 View 中每一列的源表

c++ - 连接 LPCTSTR 字符串