6

我有一个按命令排序的 select 语句。现在 order by 命令有一个 case 语句,它基于它按不同列排序的记录的状态。但是,如果状态 = 1,我还需要按 DESC 订购,否则按 ASC 订购。

我怎样才能做到这一点?

这是我目前的声明:

SELECT ph.phone_call_id AS id, ph.call_subject AS callSubject,
       ph.trigger_on AS triggerOn,
       ph.isAppointment,
       IFNULL(ph.last_attempt_on, "") last_attempt_on,
       ind.name AS industry,
       ac.account_id,
       ac.account_name AS accountName
       FROM phone_calls AS ph
       INNER JOIN accounts AS ac ON ph.account_id = ac.account_id
       INNER JOIN industries AS ind ON ind.industry_id = ac.industry_id
       INNER JOIN call_codes AS cc ON ph.call_code_id = cc.call_code_id
       WHERE ac.status = 1 
       AND ph.status = '.$call_status.' 
       AND ph.owner_id = '. USER_ID .' 
       AND ac.do_not_call = 0 
       ORDER BY CASE WHEN ph.status = 1 THEN ph.trigger_on ELSE ph.last_attempt_on END
4

1 回答 1

14

这是你想要的吗?

ORDER BY (CASE WHEN ph.status = 1 THEN ph.trigger_on  end) DESC,
         (case when ph.status <> 1 then ph.last_attempt_on END) ASC
于 2013-04-19T18:20:57.033 回答