SQL语句中case、when、then的使用

使用语法为:select `pro`.`id` AS `id`,`pro`.`title` AS `title`,`pro`.`name` AS `name`,`pro`.`ltime` AS `ltime`,`pro`.`place` AS `place`,`pro`.`linkman` AS `linkman`,`pro`.`address` AS `address`,`pro`.`tel` AS `tel`,`pro`.`email` AS `email`,`pro`.`adder` AS `adder`,`pro`.`addtime` AS `addtime`,`pro`.`remark` AS `remark`,`prep`.`addtime` AS `addtime1`,`preplog`.`addtime` AS `addtime2`,

(case

 when (`prep`.`addtime` is not null) then '1' 

when ((`preplog`.`addtime` is not null) and isnull(`prep`.`addtime`)) then '2' 

else '0' end) AS `status` 

from ((`pro` left join `prep` on((`pro`.`id` = `prep`.`pid`))) left join `preplog` on((`pro`.`id` = `preplog`.`pid`))) where (`pro`.`type` = '认领信息');

这里主要注意的问题:

(1)as关键字主要是对字段进行重命名,可以忽略不写;

(2)is not null 、is null、 isnull ()判断字段是否为空;

(3)(case  

        when     then  1

      when   then 2

     else   0  end ) as '';

  end 关键字不能缺少,as后面是对条件查询结果的重命名,也可以忽略;

(4)left join on 实现多表的连接,where 关键字是对多表链接结果的条件筛选。




你可能感兴趣的:(mysql数据库,SQL)