SQL语句,如果为空则=0,否则=1

2019-08-23  本文已影响0人  TinyThing

postgres有一种单行写法,不推荐使用

sign(COALESCE(length(mobile), 0))

还有

select
    user_id,
    case mobile
        when null then 0
        else 1 end
        as hasMobile
from tb_user;

对于mysql还有一种语法:

IF(mobile = null, 0, 1)
上一篇 下一篇

猜你喜欢

热点阅读