mysql

来源:互联网 发布:python 对象转字符串 编辑:程序博客网 时间:2024/06/07 05:55

一.

select

ifnull(p.`name`, '单点') as productname,------------------ifnull(" "," "):第一个参数为null值为单点,第一个参数不为null值则为p.name的查询值

case when c.ptype = 0 then '单点' when c.ptype = 100 then '单点' when c.ptype = 200 then '大包' end as ptype

------c.ptype查询的值为0那么赋值为(单点),c.ptype的查询值为100那么赋值为(单点),c.ptype的查询值为200那么赋值为(大包),这样写其实就是相当于控制流程

from consum_record c


二.其他举例

     select u.*, IF(u.area=1000,"我是","你是") as area  from user u;使用if:如果u.area=1000,那么返回我是,否则返回你是
     select CASE u.area WHEN 1000 THEN '男' ELSE '女' END as area from user u;使用case,when,then elese:查询u.area,如果值为1000,那么显示男否则是女,取个别名area;

    以上的例子在写SQL的时候很有用,有时候甚至可以少些其他判断代码;


oracle 其实也都差不多吧!

0 0