如何解决如何按日期对查询结果进行排序,然后按MySQL中的特定列值排序
我正在尝试按日期对查询结果进行排序,然后在每个时间范围内按状态为“已售出”,状态为“或有”,状态为“已取消”进行排序。我对ORDER BY子句没有任何反应。对于我在做错的事情,我将不胜感激。这是我现在的查询:
SELECT
subdivision community,lot_number lot,homeowner buyer,agent_initials agent,date_sold date,sales.plan plan,sales.inv_pre type,sold_price price,'sold' status,SUBSTRING_INDEX(SUBSTRING_INDEX(date_sold,'-',2),-1) month
FROM
sales
JOIN
third_party_sources
ON sales.job_id = third_party_sources.job_id
WHERE
sales.is_contingent is null
AND sales.date_sold BETWEEN :startDate AND :endDate
OR sales.is_contingent = 0
AND sales.date_sold BETWEEN :startDate AND :endDate
UNION
SELECT
subdivision community,'contingent' status,-1) month
FROM
sales
JOIN
third_party_sources
ON sales.job_id = third_party_sources.job_id
WHERE
sales.is_contingent
AND sales.date_sold BETWEEN :startDate AND :endDate
UNION
SELECT
subdivision community,buyer_name buyer,date_canceled date,null plan,null type,null price,'canceled' status,SUBSTRING_INDEX(SUBSTRING_INDEX(date_canceled,-1) month
FROM
cancellations
JOIN
third_party_sources
ON cancellations.job_id = third_party_sources.job_id
WHERE
cancellations.date_cancelled BETWEEN :startDate AND :endDate
ORDER BY
date,status = 'sold',status = 'contingent',status = 'canceled'
解决方法
我想你想要
ORDER BY date,FIELD(status,'sold','contingent','canceled')
这假定这是仅有的三个值。如果还有更多值,则您的方法将起作用。 。 。与DESC
:
ORDER BY date,(status = 'sold') DESC,(status = 'contingent') DESC,(status = 'canceled') DESC
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。