Mysql 如果列值为1,则按日期asc排序结果,否则按日期desc排序
我在mysql中有以下结果:;它只有两列:Mysql 如果列值为1,则按日期asc排序结果,否则按日期desc排序,mysql,sql-order-by,case,case-when,Mysql,Sql Order By,Case,Case When,我在mysql中有以下结果:;它只有两列:date和xdate是时间戳,x是0或1 date | x :--------- | -: 2020-02-13 | 1 2020-02-13 | 0 2020-02-01 | 1 2020-02-11 | 0 2020-02-11 | 1 2020-02-11 | 0 2020-02-12 | 1 2020-02-13 | 0 As列x排序正确,但日期与上述规定不符 另外,使用mysql。试试这个 它首先按X排序,然后按日
date
和x
date
是时间戳,x
是0
或1
date | x
:--------- | -:
2020-02-13 | 1
2020-02-13 | 0
2020-02-01 | 1
2020-02-11 | 0
2020-02-11 | 1
2020-02-11 | 0
2020-02-12 | 1
2020-02-13 | 0
As列x
排序正确,但日期与上述规定不符
另外,使用mysql。试试这个
它首先按X排序,然后按日期X=1递增,X=0递减
SELECT
`date`, `x`
FROM table1
order by
x DESC,case when x = 1 then date end,
date desc
考虑一下:
order by
x desc,
case when x = 1 then date end,
date desc
:
date | x
:--------- | -:
2020-02-01 | 1
2020-02-11 | 1
2020-02-12 | 1
2020-02-13 | 1
2020-02-13 | 0
2020-02-13 | 0
2020-02-11 | 0
2020-02-11 | 0
日期| x
:--------- | -:
2020-02-01 | 1
2020-02-11 | 1
2020-02-12 | 1
2020-02-13 | 1
2020-02-13 | 0
2020-02-13 | 0
2020-02-11 | 0
2020-02-11 | 0
当我在这个答案被删除和编辑后读到它时,我有一种强烈的似曾相识的感觉…你的主键是什么?谢谢!两个都起作用了…现在我得弄明白为什么。。。 date | x :--------- | -: 2020-02-01 | 1 2020-02-11 | 1 2020-02-12 | 1 2020-02-13 | 1 2020-02-13 | 0 2020-02-13 | 0 2020-02-11 | 0 2020-02-11 | 0