【数据库】数据库查询之关键字except,having
數(shù)據(jù)庫(kù)系列主要介紹工作和學(xué)習(xí)中遇到的一些問題,以下舉例介紹:
假設(shè)有個(gè)學(xué)生成績(jī)表 grade, 有4列:id 主鍵標(biāo)識(shí)列,學(xué)生姓名 student, 課程course, 考試成績(jī) score, 表數(shù)據(jù)如下:
?
?
| id | student | course | score |
| 1 | 張三 | 英語(yǔ) | 70 |
| 2 | 李四 | 英語(yǔ) | 81 |
| 3 | 王五 | 英語(yǔ) | 60 |
| 4 | 趙六 | 英語(yǔ) | 90 |
| 5 | 張三 | 測(cè)試 | 50 |
| 6 | 王五 | 測(cè)試 | 75 |
| 7 | 張三 | 開發(fā) | 80 |
| 8 | 王五 | 開發(fā) | 90 |
| 9 | 趙六 | 開發(fā) | 85 |
| 10 | 張三 | 數(shù)學(xué) | 85 |
| 11 | 王五 | 數(shù)學(xué) | 70 |
?
?
1.如果我們想要查詢所有課程成績(jī)都大于80分的學(xué)生姓名,應(yīng)該怎么查詢?
?
2.如果我們想要查詢選擇課程數(shù)量大于或等于2的學(xué)生姓名,應(yīng)該怎么查詢?
?
解決思路:
一、創(chuàng)建表格
?
create table grade ( id int identity(1,1), student varchar(20), course varchar(20), score int );二、插入數(shù)據(jù)
?
?
insert into grade values('張三','英語(yǔ)',70); insert into grade values('李四','英語(yǔ)',81); insert into grade values('王五','英語(yǔ)',60); insert into grade values('趙六','英語(yǔ)',90); insert into grade values('張三','測(cè)試',50); insert into grade values('王五','測(cè)試',75); insert into grade values('張三','開發(fā)',80); insert into grade values('王五','開發(fā)',90); insert into grade values('趙六','開發(fā)',85); insert into grade values('張三','數(shù)學(xué)',85); insert into grade values('王五','數(shù)學(xué)',70);?
三、查詢數(shù)據(jù)
問題1.分析:查詢的是學(xué)生姓名student,條件是每科的考試分?jǐn)?shù)都大于80分。
我們可以逆向考慮,查詢分?jǐn)?shù)小于等于80分的學(xué)生姓名
?
select distinct student from grade where score<=80因?yàn)閷W(xué)生有多門課程,當(dāng)學(xué)生多門課程的分?jǐn)?shù)滿足條件時(shí),會(huì)出現(xiàn)重復(fù)數(shù)據(jù),通過distinct關(guān)鍵詞過濾重復(fù)數(shù)據(jù)
?
只要有一門課程的分?jǐn)?shù)小于等于80,就是我們要排除的數(shù)據(jù),所以我們可以通過聯(lián)合查詢方式差集except進(jìn)行排除。
查詢所有不重復(fù)學(xué)生記錄:
select distinct student from grade;差集查詢:
?
?
select distinct student from grade except select distinct student from grade where score<=80;其實(shí)except關(guān)鍵詞已經(jīng)有去除重復(fù)數(shù)據(jù)的功能,所以最終可以優(yōu)化為:
?
?
select student from grade except select student from grade where score<=80;?
如果我不想用差集去查詢,是否還有其他方法呢?其實(shí)思路是一樣的,先查詢出分?jǐn)?shù)小于等于80分的學(xué)生姓名,然后再排除:
?
select distinct student from grade where student not in (select student from grade where score<=80); select student from grade group by student having student not in (select student from grade where score<=80); select student from grade where student not in (select student from grade where score<=80) group by student;?
第二條和第三條查詢語(yǔ)句結(jié)果是一樣的,但是having和where還是有區(qū)別,having是分組后篩選,而where是篩選后再分組。
?
問題2.分析:查詢的是學(xué)生姓名student,條件是考試的課程大于等于2。
我們知道這里student和course組合是唯一的,所以可以通過對(duì)student進(jìn)行分組,查詢到每個(gè)學(xué)生的課程數(shù)量:
?
select student,COUNT(*) as 課程數(shù)量 from grade group by student;然后可以對(duì)分組后的數(shù)據(jù)列表進(jìn)行篩選,關(guān)鍵詞having
?
select student from grade group by student having COUNT(*)>=2;?
這篇文章主要學(xué)習(xí)了聯(lián)合查詢中的差集except(oracle數(shù)據(jù)庫(kù)是minus),not in 排除方法以及分組group by和分組后篩選having.
?
?
?
?
?
?
?
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的【数据库】数据库查询之关键字except,having的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Python】Windows下Pyth
- 下一篇: IndexError: list ind