我有SQL更新查询问题.我希望能够使用针对另一个表的查询结果更新一个表.这是一个简单的例子来展示我想做的事情.
我有两张桌子:
TABLE1
ID COUNT
1 0
2 0
3 0
Table2
ID
1
1
1
2
select id,count(*) from table2 group by id;
ID COUNT
1 3
2 1
Run Code Online (Sandbox Code Playgroud)
我可以使用以下语法一次更新一行:
update table1 set count=(select count(*) from table2 where id=1 group by id) where id=1;
Run Code Online (Sandbox Code Playgroud)
我想要的是能够用一个单独的sql语句更新所有行,以便结果如下:
TABLE1
ID COUNT
1 3
2 1
3 0
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我认为这可能适合你:
update table1
set count=
(select count(*)
from table2
where table2.id=table1.id)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3146 次 |
| 最近记录: |