第四列我用了一個子查詢並命名為ssum,並且想用於兩列的相減計算,然後發現直接寫ssum並不合乎語法,想計算必須重寫一遍這個子查詢。想問一下,這種情況必須再寫一遍子查詢麼,有沒有什麼重用的方法?
select m.id,m.name,m.quantity,(select sum(c.quantity) from ctable as c where mid =m.id) as ssum,(m.quantity-(select sum(c.quantity) from ctable as c where mid =m.id)) as jian
from mtable as m
將子查詢放入臨時表