您的位置:首頁技術文章
文章詳情頁

MySQL查詢用戶表中所有記錄,按ID降序排序,如果用戶狀態為0(未激活),則注冊時間升序,排在結果最后,這個SQL.該怎么寫呢

瀏覽:88日期:2022-06-17 10:10:58

問題描述

MySQL查詢用戶表中所有記錄,按ID降序排序,如果用戶狀態為0(未激活),則注冊時間升序,排在結果最后,這個SQL.該怎么寫呢?

問題解答

回答1:

假設status=1為激活,0未激活select *from user order by status desc,case status when 1 then id end desc,case status when 0 then created_at end asc;

回答2:

select * from user order by id ASC , status ASC , register_time ASC;這樣子是你要的效果咩,如果不符合我再改

回答3:

(select from user where status=1 order by id desc) union all (select from user where status=0 order by register_time asc)

国产综合久久一区二区三区