在重复数据库中插入SELECT UPDATE UPDATE = count(*)

时间:2016-12-17 17:07:37

标签: mysql group-by sql-update sql-insert

更新某些语料库的字典中的常用术语计数。无法在单个查询中执行。相反,我将计数数据收集到临时表中,然后INSERT / UPDATE字典。想知道在单个命令中UPDATE countInCorpus的正确语法是什么。

目前的语法:

INSERT INTO temp_table (name, countInCorpus)
    SELECT name,count(*) AS theCount
    FROM corpus
    GROUP BY name having theCount > 9);

INSERT INTO dict (name, countInCorpus)
    SELECT name, countInCorpus
    FROM temp_table ON DUPLICATE KEY UPDATE dict.countInCorpus=temp_table.countInCorpus;

失败的一步语法(导致字段列表中的“未知列'theCount'”):

INSERT INTO dict (name, countInCorpus) 
    SELECT name,count(*) AS theCount
    FROM corpus
    GROUP BY name having theCount > 9
    ON DUPLICATE KEY UPDATE dict.countInCorpus=theCount;

1 个答案:

答案 0 :(得分:1)

使用VALUES()

INSERT INTO dict (name, countInCorpus) 
    SELECT name,count(*) AS theCount
    FROM corpus
    GROUP BY name havingtheCount > 9
    ON DUPLICATE KEY UPDATE dict.countInCorpus = VALUES(countInCorpus);