📜  SQLite HAVING子句

📅  最后修改于: 2020-11-13 00:31:02             🧑  作者: Mango

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;

输出: