是否可以通过案例陈述中的“DESC / ASC”更改顺序 - MySql

时间:2013-04-19 18:10:45

标签: mysql select sql-order-by

我有一个带命令的select语句。现在,order by命令具有一个案例陈述,该陈述基于由不同列排序的记录的状态。但是,如果ASC的status = 1 else命令,我还需要DESC的命令。

我该怎么做?

这是我目前的陈述:

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

1 个答案:

答案 0 :(得分:10)

这是你想要的吗?

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