阿娇开发978 发表于 2018-10-3 11:07:49

MySQL decode()的等同实现

  在Oracle中使用decode方法可以轻松实现代码和值之间的转换,但是在MySQL中该如何实现类似功能呢?
  Orcle中的实现:
  select decode(t.city_id,021,'上海',010,'北京',0532,'青岛') from user_info t;
  MySQL中没有直接的方法可以使用,但是我们可以通过下面两种方法来实现:
  1.case when then
  select case t.city_id
  when 021 then '上海'
  when 010 then '北京'
  when 532 then '青岛'
  else '中国' end
  as cityName from user_info t;
  2.if
  select if(t.city_id=021,'上海','青岛')city_id from user_info t;

页: [1]
查看完整版本: MySQL decode()的等同实现