Mysql-从 select 中更新多列

标签 mysql sql group-by

我有一个表,我需要使用另一个表中的选择来更新它。

我的想法:

UPDATE articles, 
    (SELECT  count(*) AS count FROM nots WHERE otherID=243 GROUP BY number) AS cnots 
SET articles.first=(SELECT count FROM cnots WHERE number=1),
    articles.second=(SELECT count FROM cnots WHERE number=2),
    articles.third=(SELECT count FROM cnots WHERE number=3)
WHERE articles.ID=243

查询抛出错误“表'mydb.cnots'不存在”。

我可以用 3 个不同的选择来做到这一点,但我不想这样做,有什么想法吗?

最佳答案

(SELECT  count(*) AS count FROM nots WHERE otherID=243 GROUP BY number) AS cnots 

这里这一行应该就像有一个表一样。应该是这样的

(SELECT  count(*) FROM cnots WHERE otherID=243 GROUP BY number) AS cnots

关于Mysql-从 select 中更新多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26827322/

相关文章:

php - MYSQL 数据库返回错误计数

php - Mysql php计算两个表的总价

mysql - 在单个查询中检索结果集和其他列的计数

mysql - 使用 GROUP BY 更新相关的所有记录

python - 使用一列对值进行分组,并使用 pandas 数据框返回另一列中具有最大值的值

带替换的 MySQL Pivot

php - 如何使用 PHP 检查 ip 是否已存在于 MySQL 中?

php - 'field list' php/mySQL 中的未知列,但该字段确实存在。

mysql - 选择特定日期输入的所有记录 - MySQL

python - Pandas 中的组合聚合