简体   繁体   English

结合选择和更新mysql查询以进行计数

[英]Combine select and update mysql query for count

SELECT `EAN`, COUNT(*) FROM `sales` WHERE `EAN` = '5052931107087';

UPDATE `monitoring` SET `times` = COUNT(*) WHERE `EAN` = '5052931107087';

I have these two mysql queries and I want to combine them in to one query. 我有这两个mysql查询,我想将它们组合成一个查询。 The purpose is to count the number of times an EAN code appears in the sales table and then in the monitoring table update the times count to the count from the first query. 目的是计算EAN代码在销售表中出现的次数,然后在监视表中将次数更新为第一个查询的次数。

The first query works fine and the second works fine if you replace COUNT(*) with a number but I want to understand how to pass in the count from the first query. 如果将COUNT(*)替换为数字,则第一个查询工作正常,而第二个查询工作正常,但我想了解如何从第一个查询传递计数。 As it's written at the moment it gives this error: #1111 - Invalid use of group function 正如目前所写的那样,它给出以下错误:#1111-组功能的无效使用

UPDATE
    `monitoring`
SET
    `times` = (
        SELECT
            COUNT(*)
        FROM
            `sales`
        WHERE
            `EAN` = '5052931107087'
    )
WHERE
    `EAN` = '5052931107087'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM