如何使用此查询更新table1的列?
update table1
set table1.column5 = (
select count(*)
from table2, table1
where table1.column1 = table2.column4
group by table1.column1)
table1包含这些列(column1,column2,column3,column4,column5)
table2包含这些列(column1,column2,column3,column4)
和table2.column4是table1.column1
的外键答案 0 :(得分:2)
使用此
UPDATE T1
SET
column5 = COUNT(1)
FROM table2 T2
INNER JOIN table1 T1
ON T1.column1 = T2.column4
GROUP BY T2.column4
或者这个
;WITH CNT
AS
(
SELECT
column4,
Cnt = COUNT(1)
FROM table2 T2
GROUP BY T2.column4
)
update t1
SET
column5 = CNT.Cnt
FROM CNT
INNER JOIN table1 T1
ON CNT.column4 = T1.column1
答案 1 :(得分:1)
尝试此查询
with cte as (
select column4, cnt = count(*)
from table2
group by column4
)
update a
a.column5 = b.cnt
from
table1 a
join cte b on a.column1 = b.column4