MySQL中的if和case语句使用总结

create table test(
id int primary key auto_increment,
name varchar(20),
sex int 
)
insert into test(name,sex) values('小明',1),('小兰',0),('小邹',1),('小孟',0)

select id ,name ,if(sex=1,'','') from test

select id ,name ,case sex when 1 then '' else '' end as sex from test

MySQL中的if和case语句使用总结MySQL中的if和case语句使用总结

  if([字段名]=[条件],[为真时返回],[为假时返回])

  case [字段名] when [条件] then [为真时返回] else [为假时返回] end as sex from test

   case [字段名] when [条件] then [为真时返回]  end as sex from test  (该语句为假时候返回null)

posted @ 2018-09-19 00:10 韦邦杠 阅读(...) 评论(...) 编辑 收藏

相关文章:

  • 2022-02-07
  • 2022-02-07
  • 2021-09-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-30
猜你喜欢
  • 2022-02-07
  • 2022-02-07
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案