4

我正在尝试将列日期的日期字段设置为表 1 中的“10/11/2012”,此时表 2 中与该 ID 相关的所有金额的总和(通过 fk_id)= 0。这就是我的意思:

从:

表格1

    id    date
    1     10/11/2011
    2      
    3     10/12/2011

表 2

    fk_id    amount
    1     200
    2     0
    2     0
    3     100

至:

表格1

    id    date
    1     10/11/2011
    2     10/11/2012
    3     10/12/2011

表 2

    fk_id    amount
    1     200
    2     0
    2     0
    3     100

这是我目前拥有的:

update table1
set date = '10/11/2012
FROM table1 inner join table 2 on table1.id = table2.fk_id
HAVING sum(table2.amount) = 0

有人可以帮我吗?

4

2 回答 2

5
UPDATE table1 
SET date = '10/11/2012' 
FROM table1 
WHERE id IN (SELECT FK_ID FROM table2 GROUP BY FK_ID HAVING SUM(Amount)=0)
于 2012-08-31T19:38:30.733 回答
0

这应该有效:

UPDATE T1
SET [date] = '20121011'
FROM table1 T1
INNER JOIN (SELECT fk_id, SUM(amount) Amount
            FROM table2
            GROUP BY fk_id
            HAVING SUM(amount) = 0) T2
ON T1.id = T2.fk_id
于 2012-08-31T19:39:00.543 回答