【问题标题】:INSERT FROM SELECT UPDATE ON DUPLICATES UPDATE = count(*)从重复的选择更新中插入更新 = 计数(*)
【发布时间】:2016-12-17 17:07:36
【问题描述】:

为某些语料库更新字典中的常用术语计数。无法在单个查询中执行。相反,我将计数数据收集到一个临时表中,然后插入/更新字典。想知道在单个命令中更新 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;

【问题讨论】:

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


    【解决方案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);
    

    【讨论】:

    • 工作。谢谢。 (注意有错别字——第四行缺少阈值 theCount > 9)
    猜你喜欢
    • 2011-08-06
    • 1970-01-01
    • 2016-09-17
    • 2022-01-03
    • 2013-08-02
    • 1970-01-01
    • 2016-07-04
    • 2011-09-29
    • 1970-01-01
    相关资源
    最近更新 更多