MySQL SUM IF字段b =字段a [英] MySQL SUM IF field b = field a

查看:242
本文介绍了MySQL SUM IF字段b =字段a的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SELECT 
  incMonth as Month, 
  SUM( IF(item_type IN('typ1', 'typ2') AND incMonth = Month, 1, 0 ) )AS 'Total Sales' 
FROM tester

我只需要循环使用当月的总和即可.

I just need the sum for the current month its looping through.

推荐答案

如果我没看错,则需要使用group-by或至少一个where子句来将事情限制在您所选择的时间范围内想要:

If I'm reading that right, you'd need to use a group-by or at least a where clause to restrict things to just the time range you're wanting:

SELECT incMonth AS Month, SUM(IF(item_type IN('typ1', 'typ2'), 1, 0)) AS 'Total Sales'
FROM tester
GROUP BY incMonth

这篇关于MySQL SUM IF字段b =字段a的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆