对于某些人来说,它可能看起来很简单,但我根本无法得到它
我一遍又一遍地从同一个表中有多个MS-SQL SELECT查询:
SELECT count(Page) as tAEC
FROM someTable
WHERE Page LIKE '%AEC%'
下一个
SELECT count(Page) as tTOL
FROM someTable
WHERE Page LIKE '%TOL%'
依旧......
编写此查询的更有效方法是什么。我用Google搜索了一堆类似的问题,但我无法让它们中的任何一个起作用。所以非常感谢任何帮助。
答案 0 :(得分:63)
SELECT sum(case when Page LIKE '%AEC%' then 1 end) as tAEC,
sum(case when Page LIKE '%TOL%' then 1 end) as tTOL
FROM someTable
答案 1 :(得分:6)
您可以使用GROUP BY Page
SELECT
Page
, COUNT(Page) as "nb"
FROM someTable
GROUP BY Page
您还可以执行GROUP BY CASE...
SELECT
CASE
WHEN Page LIKE '%AEC%' THEN "AEC"
WHEN Page LIKE '%TOL%' THEN "TOL"
ELSE ""
END AS "type"
, count(Page) as "nb"
FROM someTable
GROUP BY type
您还可以COUNT IF
SELECT
COUNT(IF(Page LIKE '%AEC%', 1, NULL) THEN "AEC"
, COUNT(IF(Page LIKE '%TOL%', 1, NULL) THEN "TOL"
FROM someTable