[英]MYSQL sum based on if condition from other table in left join
我發現的總和total
表customer_class_bookings
基於IF
來自其他表的條件trader_job_payment
。 我的問題是它給出了 0,這是錯誤的。 這是我的代碼
select SUM(IF (trader_job_payment.status = 'DISBURSED' , customer_class_bookings.total, 0))
AS earning from `customer_class_bookings`
left join `trader_job_payment` on `trader_job_payment`.`payable_id` = `customer_class_bookings`.`id` and
`trader_job_payment`.`payable_type` = 'Modules\\WellBeing\\Models\\CustomerClassBooking' and `trader_job_payment`.`deleted_at` = null where `trader_user_id` = 917
earning
為0,這是錯誤的。 任何機構都可以幫助我解決這個問題。 謝謝
您正在檢查值“null”而不是檢查單元格是否為 NULL。 Null 不是 MySql 中的值。 你必須像這樣使用 IS NULL 函數:
SELECT *
FROM contacts
WHERE last_name IS NULL;
所以你的查詢將變成(僅修改部分):
`trader_job_payment`.`deleted_at` IS NULL
您可以 在此處了解有關如何處理 NULL 的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.