mysql case when then else end 写法

今天改SQL用到了就搜了搜,现在记下来。

case

when tableName.type in (1,2,3,4)

and tableName.state  in (5,6)

then 1

when tableName.type in (7,8)

and tableName.state in (9,10)

then 1

else 0

end

如果要用到聚合函数就可以如

sum (

case

when tableName.type in (1,2,3,4)

and tableName.state  in (5,6)

then 1

when tableName.type in (7,8)

and tableName.state in (9,10)

then 1

else 0

end

) review

 

posted @ 2018-03-12 16:54  LittleAnts  阅读(157)  评论(0编辑  收藏  举报