Home > Backend Development > PHP Tutorial > mysql排序问题~

mysql排序问题~

WBOY
Release: 2016-06-20 12:31:26
Original
983 people have browsed it

我现在有两个字段 addDay(添加时间)和status(状态)。
我想要实现以下排序方式:
1.按addDay降序排列,并且status升序排列且status2.把status>=6的放在规则1的后面,并且按addDay降序和status升序排列

addDay         status
2016-4-14    1
2016-4-14    2
2016-4-13    1
2016-4-13    2
2016-4-14    6
2016-4-13    6

请问各位大神,这样的排序,应该如何写sql语句?


回复讨论(解决方案)

select addDay,status from table where status < 6 order by addDay desc,status ascunion allselect addDay,status from table where status >= 6 order by addDay desc,status asc
Copy after login

order by case when status<6 then addDay else 1 end desc,status asc
Copy after login

order by case when status<6 then addDay else 1 end desc,addDaydesc,status asc
Copy after login

select * from table order by if(status<6 , 1, 0 ) desc, addDay desc,status asc

select * from tbl_name order by status>=6, addDay desc, status asc
Copy after login

谢谢各位了,问题已经解决了

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template