SELECT
table_1.id,
(SELECT SUM(amount) FROM table_2 WHERE table_1.id = table_2.bid) AS sum_total
FROM table_1
WHERE table_1.total!=0
我需要更新table_1.total
,以便它等於sum_total
。MySQL更新根據選擇
任何adivce?
SELECT
table_1.id,
(SELECT SUM(amount) FROM table_2 WHERE table_1.id = table_2.bid) AS sum_total
FROM table_1
WHERE table_1.total!=0
我需要更新table_1.total
,以便它等於sum_total
。MySQL更新根據選擇
任何adivce?
如下嘗試:
update table_1 as t1 left join (select SUM(amount) as totalamount ,id FROM table_1 group by id) as t2 on t1.id =t2.id
set t1.total=t2.totalamount
請不要要緊的變化。
這是否對你的工作:
UPDATE table_1 set total = (SELECT SUM(amount) FROM table_1 WHERE table_1.id = table_1.bid) AS sum_total)
不完全,我只是更新了我的基地選擇查詢 - 有兩個錯誤。 –
這個簡單的查詢應該工作得很好。
UPDATE
table_1
SET
table_1.total = (SELECT SUM(amount) FROM table_2 WHERE table_1.id = table_2.bid)
WHERE table_1.total!=0;
這是真的可行。你甚至嘗試過自己嗎? –
真的不在這個時候。但我在我的工作中做過類似的查詢。如果有任何更改,請讓我更新 – 2011-11-18 07:17:28
在更新期間,我們可以在FROM子句中使用與目標名稱相同的表嗎?請看看。 –