mysql update field_mysql-更新表与另一个选择,但字段是SUM(someField)
基本上我有這樣的事情:
UPDATE
Table
SET
Table.col1 = other_table.col1,
FROM
Table
INNER JOIN
other_table
ON
Table.id = other_table.id
問題是我想用如下選擇來更新col1:
SELECT SUM(col1) FROM other_table WHERE Table.id = other_table.id AND period > 2011
編輯
正確答案:
UPDATE bestall
INNER JOIN (SELECT bestid,SUM(view) as v,SUM(rawView) as rv
FROM beststat
WHERE period > 2011 GROUP BY bestid) as t1
ON bestall.bestid = t1.bestid
SET view = t1.v, rawview = t1.rv
解決方法:
您不能在set子句中直接使用聚合.解決該問題的一種方法是子查詢:
update your_table as yt
left join
(
select id
, count(*) as cnt
from other_table
where period < 4
group by
id
) as ot
on yt.id = ot.id
set col1 = coalesce(ot.cnt,0)
標簽:sql,mysql
來源: https://codeday.me/bug/20191201/2080765.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的mysql update field_mysql-更新表与另一个选择,但字段是SUM(someField)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用photoshop需要什么配置电脑?
- 下一篇: 运行photoshop对电脑配置的要求是