我有一个SQL:

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_count为1,我想选择deals.partner_id=62039的位置。
如果partner_count不是1,那么它应该选择refnumbers.claim_track_id=62039的位置
怎么能做到?

最佳答案

尝试

where
case when deals.partner_count = 1
   then deals.partner_id = 62039
   else refnumbers.claim_track_id = 62039
end

关于mysql - MySQL查询取决于列值定义where语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14379742/

10-11 11:08