SQLite HAVING子句
SQLite HAVING子句用于指定过滤出现在最终结果中的分组结果的条件。WHERE子句会对选定的列进行条件判断,而HAVING子句则对由GROUP BY子句创建的分组进行条件判断。
HAVING子句在SELECT查询中的位置:
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
语法:
SELECT column1, column2
FROM table1, table2
WHERE [ conditions ]
GROUP BY column1, column2
HAVING [ conditions ]
ORDER BY column1, column2
示例:
让我们举一个例子来演示HAVING子句。我们有一张名为”STUDENT”的表,其中包含以下数据:
示例1:
显示所有名称计数少于2的记录:
SELECT * FROM STUDENT GROUP BY NAME HAVING COUNT(NAME) < 2;
输出:
示例2:
显示所有名称计数大于2的记录:
SELECT * FROM STUDENT GROUP BY NAME HAVING COUNT(NAME) > 2;
输出: