mysql – 访问SQL Count字段,其中equals值
作者:互联网
我正在为一个客户的桌子工作,他们只希望计算特定的列值我已经采用了一种方式,但它不是他们需要的结果,所以它不在一边.
我有这样的表.(注:不是实际的表)
company_name | type | ................
com A | type1 | ................
com A | type2 | ................
com B | type1 | ................
com A | type2 | ................
com A | type1 | ................
com C | type1 | ................
com C | type2 | ................
com A | type1 | ................
所以在类型列中如此,尽管其中有列,它们根本不需要计数
所需的表格如下所示
company_name | Type1 | Type2 | ..........
com A | 3 | 2 | ..........
com B | 1 | 0 | ..........
com C | 1 | 1 | ..........
到目前为止我得到的是:
company_name | Type | count | ..........
com A | type1 | 3 | ..........
com A | type2 | 2 | ..........
com B | type1 | 1 | ..........
com C | type1 | 1 | ..........
com C | type2 | 1 | ..........
请帮助我一直在抓我的头骨.
解决方法:
试试这个:
SELECT
company_name,
SUM(IIF(Type = 'type1', 1, 0)) AS Type1,
SUM(IIF(Type = 'type2', 1, 0)) AS Type2,
SUM(IIF(Type = 'type3', 1, 0)) AS Type3
FROM table
GROUP BY company_name;
更新
在MySQL中,您可以使用IF
语句而不是Access的IIF,也可以在没有IF或CASE的情况下编写它,因为MySQL隐式转换:
SELECT Promoter,
Sum(NoticeType = 'VARIATION') AS 'Variation',
Sum(NoticeType = 'TWO HOURS AFTER') AS 'Two Hours After',
Sum(NoticeType = 'THREE MONTHS') AS 'Three Months',
Sum(NoticeType = 'THREE DAY') AS 'Three Day',
Sum(NoticeType = 'TEN DAY') AS 'Ten Day'
FROM notices
GROUP BY Promoter;
标签:mysql,sql,ms-access-2007 来源: https://codeday.me/bug/20190520/1143778.html