这是我的数据;
type valid blocked
--------- ----- --------
CPI 0 0
CPI 1 0
CPI 0 1
CPI 1 1
CPA 0 0
CPA 1 0
CPA 0 1
CPA 1 1
CPE 0 0
CPE 1 0
CPE 0 1
CPE 1 1
PIN 0 0
PIN 1 0
PIN 0 1
PIN 1 1
这是我想要的排序方式;
type valid blocked
--------- ----- --------
CPI 1 0
CPA 1 0
CPI 1 1
CPA 1 1
CPI 0 0
CPA 0 0
CPI 0 1
CPA 0 1
CPE 1 0
CPE 1 1
CPE 0 0
CPE 0 1
PIN 1 0
PIN 1 1
PIN 0 0
PIN 0 1
因此,首先是CPI和CPA,按有效排序然后解锁。接下来,其CPI按有效排序,然后取消阻止,最后按PIN排序,再按有效和未阻止排序。
甚至可以在一个查询中对此进行排序吗?感谢。
答案 0 :(得分:1)
select * from your_table
order by case when type in ('CPI','CPA') then 1
when type = 'CPE' then 2
when type = 'PIN' then 3
end,
valid <> 0,
blocked
答案 1 :(得分:0)
select * from your_table
order by `type` in ('CPI','CPA') desc
, `type` = 'CPE' desc
, `type` = 'PIN' desc
, valid desc, blocked
, `type` = 'CPI' desc
;