我有兩個數據庫表:SQL SUM和比較
***aff_purchases***
id | affiliate_id | payout
1 | 12 | 50.00
2 | 12 | 10.00
3 | 12 | 50.00
4 | 12 | 10.00
***aff_payments***
id | affiliate_id | amount_paid
8 | 12 | 50.00
我想返回所有的會員的ID在「支出」一共是50或大於「amount_paid」一個會員ID更多的數組。
我認爲我需要彙總列然後比較,但我很努力去理解如何。請參閱我的努力之下:
SELECT
(SELECT SUM(amount_paid) FROM exp_cdwd_aff_payments AS pay WHERE pay.affiliate_id = 12) AS 'amount_paid'
(SELECT SUM(payout) FROM exp_cdwd_aff_purchases AS pur WHERE pur.affiliate_id = 12) AS 'payout'
FROM
exp_cdwd_aff_payments AS pay
WHERE
payout > amount_paid
你想要返回什麼? –