我有這樣的SQL:MySQL查詢具體取決於列的值來定義WHERE語句
SELECT
`refnumbers`.`order_id`,
`refnumbers`.`deal_id`,
`refnumbers`.`claim_track_id`,
`deals`.`partner_count`
FROM `refnumbers`
JOIN `deals`
ON (`refnumbers`.`deal_id` = `deals`.`ID`)
JOIN `users`
ON (`users`.`id` = `deals`.`partner_id`)
WHERE `refnumbers`.`is_claimed` = '1'
AND `deals`.`partner_id` = '62039'
AND `refnumbers`.`claimed_at` BETWEEN '2013-01-17 00:00:00'
AND '2013-01-17 23:59:59'
ORDER BY `refnumbers`.`claimed_at` DESC
我想選擇其中deals.partner_id = 62039 IF deals.partner_count爲1
如果未啓用PARTNER_COUNT 1那麼它應該選擇refnumbers.claim_track_id = 62039
這怎麼辦?
可以使用條件的MySQL關鍵字CASE WHEN。 .. THEN ... END – bodi0