sql语句中使用if改变列的值和sql语句根据月份分组并判断高级条件的语法

1.sql语句中使用if改变列的值和sql语句根据月份分组并判断高级条件的语法

(1)使用if改变列的值的sql语句

SELECT
	sum(( ( IF ( plan_invested_amount_centre IS NULL, 0, plan_invested_amount_centre ) ) * ( IF ( invest_money_unit_centre = 2, 6.992, 1 ) ) ) + ( ( IF ( plan_invested_amount_outer IS NULL, 0, plan_invested_amount_outer ) ) * ( IF ( invest_money_unit_outer = 2, 6.992, 1 ) ) ) )
	 AS money 
FROM
	project_info 
WHERE
	is_delete = 0 
	AND STATUS = 0 
	AND is_recycle = 0 
	AND ( ( step1 = 3 AND step2 >= 2 ) OR step1 = 4 OR step1 = 5 )
	and negotiation_time >= '2020-01-13 18:27:46'
            and '2020-09-13 18:27:46' >= negotiation_time

大致解释:if括号里面的数据,第一个参数是判断语句,第二个是判断语句成立时列要变成的值,第三个参数是判断语句不成立时列要变成的值

(2)根据月份分组并判断高级条件的sql语句

SELECT
	DATE_FORMAT( negotiation_time, '%Y-%m' ) AS MONTH,
	sum(
	( ( IF ( plan_invested_amount_centre IS NULL, 0, plan_invested_amount_centre ) ) * ( IF ( invest_money_unit_centre = 2, 6.992, 1 ) ) ) + ( ( IF ( plan_invested_amount_outer IS NULL, 0, plan_invested_amount_outer ) ) * ( IF ( invest_money_unit_outer = 2, 6.992, 1 ) ) ) 
	) AS money 
FROM
	project_info 
WHERE
	is_delete = 0 
	AND STATUS = 0 
	AND is_recycle = 0 
	AND ( ( step1 = 3 AND step2 >= 2 ) OR step1 = 4 OR step1 = 5 ) 
	AND negotiation_time IS NOT NULL 
GROUP BY
	DATE_FORMAT( negotiation_time, '%Y-%m' );

大致解释:根据月份分组,DATE_FORMAT( date, '%Y-%m' );就是把date的时间变成2020-01这样的格式

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值