简体   繁体   English

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

[英]Is it possible to change the order by “DESC/ASC” in a case statment - MySql

I have a select statement with a order by command. 我有一个带命令的select语句。 Now the order by command has a case statment based on the status of the record it sort by a different column. 现在,order by命令具有一个案例陈述,该陈述基于由不同列排序的记录的状态。 However, I need to also the order by DESC if the status = 1 else order by ASC. 但是,如果ASC的status = 1 else命令,我还需要DESC的命令。

How can I do this? 我怎样才能做到这一点?

This is my current statement: 这是我目前的陈述:

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

Is this what you want? 这是你想要的吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM