在 mysql 中

  1. group by ,having ,order by 支持字段的别名。
    group by ,order by 后使用别名
    select
    case
    when t.author_level in(1,2) then '1-2级'
    when t.author_level in(3,4) then '3-4级'
    when t.author_level in(5,6) then '5-6级'
    else ''
    end as level_cnt,
    count(s.issue_id) as num
    from author_tb t join answer_tb s on t.author_id = s.author_id
    where s.char_len >= 100
    group by level_cnt
    order by num desc
  2. where 后面不支持字段的别名。
  3. 如果关联查询的时候给表起了别名。那么 where 后面如果用’表名.字段名’的话这个表名要用表的别名

  4. case 关键字后面可以接表的别名

    select
    case record.xingbie
    when 1 then '上行'
    when 2 then '下行'
    when 3 then '出段'
    when 4 then '入段'
    end as xingbieName
    from
    t_sjfx_mh_jcjl record
    left join sys_dict_single lineDict on lineDict.value = record.xianbie