SQLite Having 子句
HAVING 子句允許指定條件來篩選將出現在最終結果中的分組結果。
WHERE 子句在所選列上設定條件,而 HAVING 子句則在 GROUP BY 子句建立的分組上設定條件。
語法
下面是HAVING 子句在SELECT 查詢中的位置:
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
FROM
WHERE
GROUP BY
HAVING
ORDER BY
在一個查詢中,HAVING 子句必須放在GROUP BY 子句之後,必須放在ORDER BY 子句之前。以下是包含HAVING 子句的SELECT 語句的語法:
SELECT column1, column2
FROM table1, table2
WHERE [ conditions ]lum #UP BY column1, HAVING [ conditions ]
ORDER BY column1, column2
#10 James 45 Texas #1
FROM table1, table2
WHERE [ conditions ]lum #UP BY column1, HAVING [ conditions ]
ORDER BY column1, column2
#
實例
假設COMPANY 表有以下記錄:
ID NAME AGE ---- ---------- ---------- ----------
1 Paul 32 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0
8 Paul 24 Houston 20000.0
9 James 44 James 45 Texas 5000.0
1 Paul 32 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0
8 Paul 24 Houston 20000.0
9 James 44 James 45 Texas 5000.0
#12的所有記錄名稱》 ##sqlite > SELECT * FROM COMPANY GROUP BY name HAVING count(name) < 2;
###這將產生以下結果:
ID NAME AGE ------ ---------- ----------
2 Allen 25 David 27 Texas 855000
6 South- Hall 45000
4 Mark 25 23 Norway ‧
#sqlite > SELECT * FROM COMPANY GROUP BY name HAVING count(name) > 2;
# ADDRESS SALARY
---------- ---------- ---------- ---------- ----- -----2 Allen 25 David 27 Texas 855000
6 South- Hall 45000
4 Mark 25 23 Norway ‧
#sqlite > SELECT * FROM COMPANY GROUP BY name HAVING count(name) > 2;
# ADDRESS SALARY