1、復(fù)雜SQL查詢
1.1、單表查詢
(1)選擇指定的列
[例]查詢?nèi)w學(xué)生的學(xué)號和姓名
select Sno as 學(xué)號,Sname as 姓名 from student;
select Sno,Sname from student;
(2)查詢?nèi)苛?/p>
[例]查詢?nèi)w學(xué)生的詳細(xì)信息
(3)對查詢后的指定列進(jìn)行命名
[例]查詢?nèi)繉W(xué)生的“姓名”及其“出生年”兩列
select Sname as 姓名,(2014-Sage) as 出生年 from student;
select Sname ,(2014-Sage) from student;
(4)消除取值重復(fù)的行
[例]查詢選修了課程的學(xué)生學(xué)號
select distinct Sno as 選修了課程的學(xué)生學(xué)號 from SC;
select distinct Sno from SC;
(5)選擇表中若干元組(滿足條件的)
1.2、大小比較
[例]查詢計算機(jī)系(IS)全體學(xué)生名單
select Sname as 學(xué)生姓名 from student where Sdept='IS';
[例]查詢?nèi)w20歲以下的學(xué)生姓名和年齡
select Sname as 姓名,Sage as 年齡 from student where Sage20;
1.3、確定范圍
[例]查詢所有在20到23歲(含20和23)的學(xué)生姓名、系別和年齡
select Sname as 姓名,Sdept as 系別,Sage as 年齡 from student where Sage between20 and 23;
注意between 小數(shù) and 大數(shù)。
1.4、in和not in確定集合
[例]查詢IS系和CS系的全體學(xué)生姓名和性別
select Sname as 姓名,Ssex as 性別 from student where Sdept='IS' or Sdept='CS';
select Sname as 姓名,Ssex as 性別 from student where Sdept in ('IS','CS');
[例]查詢既不屬于IS系,也不屬于MA系的學(xué)生姓名和年齡
select Sname as 姓名,Sage as 年齡 from student where Sdept !='IS'and Sdept!='CS';
select Sname as 姓名,Sage as 年齡 from student where Sdept not in('IS','MA');
1.5、字符匹配(like % _ )
[例]查詢所有姓李的學(xué)生姓名和性別
select Sname as 姓名,Ssex as 性別 from student where Sname like '李%';
[例]查詢所有“2002”年入學(xué)的學(xué)生學(xué)號、姓名和系別
select Sno as 學(xué)號,Sname as 姓名,Sdept as 系別 from student where Sno like'2002%';
[例]查詢所有不姓“劉”的學(xué)生信息
select * from student where Sname not like'劉%';
[例]查詢名稱含有“數(shù)據(jù)”的課程號、課程名及學(xué)分
select Cno as 課程號,Cname as 課程名,Ccredit as 學(xué)分 from course where Cname like '%數(shù)據(jù)%';
總結(jié):
select * from course where cname like '%數(shù)據(jù)%';包含數(shù)據(jù)的字符串
select * from course where cname like '數(shù)據(jù)%';以數(shù)據(jù)開頭的字符串
select * from course where cname like '%數(shù)據(jù)'; 以數(shù)據(jù)結(jié)尾的字符串
1.6、涉及空值的查詢(is null)
[例]查詢沒有先修課的課程號和課程名
select Cno as 課程號,Cname as 課程名,Cpno from course where Cpno is null;
[例]查詢所有有成績的學(xué)生學(xué)號、課程號及成績
select Sno as 學(xué)號,Cno as 課程號,Grade as 成績 from SC where Grade is not null;
1.7、查詢結(jié)果排序(order by )
[例]查詢選修了3號課程的學(xué)生學(xué)號和成績,結(jié)果按成績降序排列。
select Sno as 學(xué)號,Grade as 成績 from SC where Cno=3 order by Grade desc;
[例]查詢選修了3號課程的學(xué)生學(xué)號和成績,結(jié)果按成績升序排列。
select Sno as 學(xué)號,Grade as 成績 from SC where Cno=3 order by Grade asc;
1.8、聚集函數(shù)
count、sum、avg、max、min
[例]查詢學(xué)生總數(shù)
select count(*) as 學(xué)生總數(shù) from student;
[例]查詢所有課程的總學(xué)分
select sum(Ccredit) as 所有課程總學(xué)分 from course;
[例]查詢?nèi)w學(xué)生平均年齡
select avg(Sage) as 平均年齡 from student;
[例]查詢1號課程的最高分
select max(Grade) as 1號課程的最高分 from SC where Cno=1;
1.9、分組統(tǒng)計(group by)
[例]查詢男女學(xué)生各有多少人。
select Ssex as 性別,count(*) as 人數(shù) from student group by Ssex;
[例]查詢每個課程的課程號和平均分。
select Cno as 課程號,avg(Grade) as 平均分 from SC group by Cno;
【例】查詢選修了3門課程以上(含3門)的學(xué)生學(xué)號和選修課程數(shù)。
select Sno as 學(xué)號 ,count(course.Cno) as 選修課程數(shù)
From SC,course
Where course.Cno=SC.Cno
Group by Sno
Having Count(course.Cno)>=3;
having 關(guān)鍵字后面直接跟聚集函數(shù)
在 SQL 中增加 HAVING 子句原因是,WHERE 關(guān)鍵字無法與合計函數(shù)一起使用。
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value
【例】查詢選修了2門課程以上(含2門,但不含1號課程),學(xué)生學(xué)號和選修課程數(shù)。
select Sno as 學(xué)號 ,count(course.Cno) as 選修課程數(shù)
From SC,course
Where course.Cno=SC.Cno and course.Cno !=1
Group by Sno
Having Count(course.Cno)>=2;
【例】查詢不及格門數(shù)2門以上的學(xué)生學(xué)號。
Select Sno
from sc
Where sc.Grade60
Group by Sno
Having count(Cno)>=2;
【例】查詢有2名以上(含2名)學(xué)生選修了的課程號和選修人數(shù)。
Select Cno,count(Sno)
From SC
Group by Cno
Having count(sno)>=2
2、連接查詢
(1)等值與非等值連接查詢
[例]查詢每個學(xué)生及其的選修課程情況
select student.Sno as 學(xué)號,course.Cno as 選修課號,SC.Grade as 成績
from student,course,SC
where student.Sno=SC.Sno and course.Cno=SC.Cno ;
(2)自身連接
[例]查詢每個學(xué)生的間接選修課
select SC.Sno as 學(xué)號,
FIRST.Cname as 直接選修課,
SECOND.Cname as 間接選修課
from SC,
course as FIRST,
course as SECOND
where FIRST.Cno=SC.Cno
and FIRST.Cpno=SECOND.Cno;
(3)外連接
[例]查詢所有學(xué)生選修課程情況(含沒選修課程的學(xué)生)
select student.Sno as 學(xué)號,
Sname as 姓名,
sc.Cno as 選修課程號
from student
LEFT OUTER JOIN SC ON student.Sno=SC.Sno;
join 用于根據(jù)兩個或多個表中的列之間的關(guān)系,從這些表中查詢數(shù)據(jù)
JOIN: 如果表中有至少一個匹配,則返回行
LEFT JOIN: 即使右表中沒有匹配,也從左表返回所有的行
RIGHT JOIN: 即使左表中沒有匹配,也從右表返回所有的行
FULL JOIN: 只要其中一個表中存在匹配,就返回行
UNION 操作符用于合并兩個或多個 SELECT 語句的結(jié)果集。
請注意,UNION 內(nèi)部的 SELECT 語句必須擁有相同數(shù)量的列。列也必須擁有相似的數(shù)據(jù)類型。同時,每條 SELECT 語句中的列的順序必須相同。
3 、嵌套查詢
(1)帶有IN謂詞的子查詢( 屬性 in (子查詢的查詢結(jié)果) )
【例】查詢與王敏同學(xué)在同一個系的學(xué)生信息。
select *
from student
where Sdept in (
select Sdept
from student
where Sname='王敏'
);
【例】查詢不與王敏同學(xué)不在同一個系的學(xué)生信息。
select *
from student
where Sdept not in (
select Sdept
from student
whereSname='王敏'
);
【例】查詢選修了課程名是“信息系統(tǒng)”的學(xué)生學(xué)號和姓名。
select student.Sno as 學(xué)號, Sname as 姓名
from student,SC
where student.Sno=SC.Sno and Cno in (
select Cno
from course
where Cname='信息系統(tǒng)'
)
【例】查詢曾與劉晨一同上課的學(xué)生學(xué)號和姓名。(假設(shè):一個課程只有一個上課班)
select distinct student.Sno as 學(xué)號, Sname as 姓名
from student,SC
where student.Sno=SC.Sno and Cno in (
select Cno
from SC,student
where SC.Sno=student.Sno and student.Sno in (
select Sno
from student
where student.Sname='劉晨'
)
)
- 內(nèi)層in 查出劉晨的學(xué)號sno,外層in查出劉晨所上課程的課程號。
(2)帶有比較運(yùn)算符的子查詢(=,>=,=,>或!=)
【例】查詢與王敏同學(xué)在同一個系的所有學(xué)生信息 (=判斷)
select *
from student
where Sdept=(
select Sdept
from student
where Sname='王敏'
)
【例】查詢每個學(xué)生超過該課程最低分的課程號。(同類課程不是最低分的),子查詢的結(jié)果返回一個數(shù)的時候,這個子查詢就可以當(dāng)一個數(shù)用?可以使用in符號,或者大于小于符號。
select Cno
from SC a
where Grade> (
select min(Grade)
from SC b
where a.Cno=b.Cno
)
【例】查詢每個學(xué)生超過他選修課程平均成績的課程號。
select Cno
from SC a
where Grade> (
select avg(Grade)
from SC b
where a.Sno=b.Sno
)
(3)帶有ANY或ALL謂詞的子查詢
- ANY表示任何一個,ALL表示所有,可以用在子查詢的括號前面
【例】查詢其他系中比計算機(jī)系某一學(xué)生年齡小的學(xué)生姓名,性別、年齡和所在系。
select Sname as 姓名,Ssex as 性別, Sage as 年齡, Sdept as 所在系
from student
where Sage (
select Sage
from student
where Sdept='CS'
);
【例】查詢其他系中比計算機(jī)系所有年齡都小的學(xué)生姓名和年齡。
select Sname as 姓名, Sage as 年齡
from student
where Sdept>'CS' and Sage ALL (
select Sage
from student
where Sdept='CS'
);
(4 )帶有Exists謂詞的子查詢
【例】查詢所有選修了1號課程的學(xué)生姓名。
select Sname as 姓名
from student
where Exists (
select *
from SC
where Cno=1 and Sno=Student.Sno
);
4、集合查詢
(1)并UNION
【例】 查詢計算機(jī)系的學(xué)生及年齡不大于19歲的學(xué)生詳細(xì)信息。
select *
from student
where student.Sdept='CS'
union
select *
from student
where student.Sage=19;
(2)交INTERSECT
【例】查詢選修了1號課程的與年齡不大于19歲的 學(xué)生 詳細(xì)信息 的交集。
Select *
from student,SC
where student.Sno=SC.Sno and SC.Cno=1
INTERSECT
Select *
from student
where student.Sage=19;
(3)差EXCEPT
【例】查詢計算機(jī)科學(xué)系的學(xué)生與年齡不大于19歲的學(xué)生詳細(xì)信息的差集。
select *
from student
where student.Sdept='SC'
EXCEPT
select *
from student
where student.Sage=19;
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
您可能感興趣的文章:- MySQL查詢重寫插件的使用
- 一篇文章弄懂MySQL查詢語句的執(zhí)行過程
- Python使用sql語句對mysql數(shù)據(jù)庫多條件模糊查詢的思路詳解
- MySQL 數(shù)據(jù)庫 like 語句通配符模糊查詢小結(jié)
- 淺談pymysql查詢語句中帶有in時傳遞參數(shù)的問題
- MySQL模糊查詢語句整理集合
- mysql語句查詢用戶權(quán)限過程詳解
- SQL語句執(zhí)行深入講解(MySQL架構(gòu)總覽->查詢執(zhí)行流程->SQL解析順序)
- MySQL 重寫查詢語句的三種策略