码迷,mamicode.com
首页 > 数据库 > 详细

sql语句if,case when

时间:2017-09-15 10:07:50      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:span   expr   pen   account   sel   bsp   blog   mount   nbsp   

1.if

表达式:IF( expr1 , expr2 , expr3 )

expr1条件,条件为true,则值是expr2 ,false,值就是expr3 

例:

SELECT 
o.id,u.account,catagory.`name`,orderTime,detail.amount,periodtime,if(direction=‘0‘,‘看涨‘,‘看跌‘) directionName
FROM t_order_detail detail
LEFT JOIN t_order o ON o.id = detail.orderId
LEFT JOIN t_catagory catagory ON catagory.id = o.catId
LEFT JOIN t_user u ON u.id = o.userId

2.case when

case 列名

when 条件 then 结果

else 其它结果

end 别名

例:

SELECT 
CASE detail.`status` WHEN ‘0‘ THEN ‘未开仓‘
WHEN ‘1‘ THEN ‘已开仓‘
WHEN ‘2‘ THEN ‘已平仓‘
ELSE ‘取消订单‘ END
status,
CASE o.type WHEN ‘0‘ THEN ‘单期‘
WHEN ‘1‘ THEN ‘多期‘
ELSE ‘策略‘ END 
typeName
FROM t_order_detail detail
LEFT JOIN t_order o ON o.id = detail.orderId

 

sql语句if,case when

标签:span   expr   pen   account   sel   bsp   blog   mount   nbsp   

原文地址:http://www.cnblogs.com/-scl/p/7524479.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!