zoukankan      html  css  js  c++  java
  • sql语句if,ifnull,case when

    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

     3.ifnull

    IFNULL( expr1 , expr2 )

    在 expr1 的值不为 NULL的情况下都返回 expr1,否则返回 expr2

    select IFNULL(detailcode,'无') detailcode from ...
  • 相关阅读:
    2019年下半年学习总结
    要看的积累
    【长期积累】Java
    【长期积累】数据库
    一些知识总结
    一些小总结
    优秀前端框架Semantic UI
    windows nodejs express的安装
    形象的讲解angular中的$q与promise(转)
    centos6.5 install mongodb
  • 原文地址:https://www.cnblogs.com/-scl/p/7524479.html
Copyright © 2011-2022 走看看