Home  >  Q&A  >  body text

MySQL查询用户表中所有记录,按ID降序排序,如果用户状态为0(未激活),则注册时间升序,排在结果最后,这个SQL.该怎么写呢

MySQL查询用户表中所有记录,按ID降序排序,如果用户状态为0(未激活),则注册时间升序,排在结果最后,这个SQL.该怎么写呢?

ringa_leeringa_lee2743 days ago716

reply all(3)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 15:57:51

    Assume status=1 is activated, 0 is not activated
    select *from user order by status desc,case status when 1 then id end desc,case status when 0 then created_at end asc;

    reply
    0
  • 阿神

    阿神2017-04-17 15:57:51

    select * from user order by id ASC , status ASC , register_time ASC;This is the effect you want, if it doesn’t suit me I will change it

    reply
    0
  • 迷茫

    迷茫2017-04-17 15:57:51

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

    reply
    0
  • Cancelreply