我有一个表,我需要使用另一个表中的select来更新它。
我的想法:
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种不同的选择来做,但我不想,有什么想法吗?
答案 0 :(得分:0)
(SELECT count(*) AS count FROM nots WHERE otherID=243 GROUP BY number) AS cnots
这里的这一行应该好像有一个表作为cnots。这应该是这样的
(SELECT count(*) FROM cnots WHERE otherID=243 GROUP BY number) AS cnots
答案 1 :(得分:0)
怎么样
- >
UPDATE articles
SET articles.first=(SELECT cnots.count
FROM
(SELECT count(*) AS count
FROM nots WHERE otherID=243 GROUP BY number) AS cnots
WHERE number=1)
.......
答案 2 :(得分:0)
好的,这是我发现的最佳解决方案:
update articles,
(SELECT sum(case when number = 1 then 1 else 0 end) AS first,
sum(case when number = 2 then 1 else 0 end) AS second,
sum(case when number = 3 then 1 else 0 end) AS third
FROM nots WHERE otherID=243 ) AS cnots
SET articles.first=cnots.first,
articles.second=cnots.second,
articles.third=cnots.third
WHERE articles.ID=243