MySQL常用SQL(含復雜SQL查詢)


1、復雜SQL查詢

 

1.1、單表查詢

(1)選擇指定的列

[例]查詢全體學生的學號和姓名

 

  1. select Sno as 學號,Sname as 姓名 from student;
  2. select Sno,Sname from student;

(2)查詢全部列

[例]查詢全體學生的詳細信息

select * from student;

(3)對查詢后的指定列進行命名

[例]查詢全部學生的“姓名”及其“出生年”兩列

  1. select Sname as 姓名,(2014-Sage) as 出生年 from student;
  2. select Sname ,(2014-Sage) from student;

第一種:

一張人員信息表里有一人生日(Birthday)列,跟據這個列,算出該人員的年齡

datediff(year,birthday,getdate())

例:birthday = '2003-3-8'

getDate()= '2008-7-7'

結果為:5

這樣結果是會返回該人員的大概年齡,但不精確.不會精確到月或日.

按照上面測試的日期,該人員的實際年齡應該還不滿5歲。在需要精確年齡的時候,就會有錯.

第二種:

  FLOOR(datediff(DY,birthday,getdate())/365.25)

FLOOR函數:

FLOOR(expr) 返回小於或等於expr的最大整數.FLOOR(1.1)返回1,FLOOR(-1.1)返回-2,FLOOR(1)返回1

這樣就可以精確算出,該人員當前實際年齡了.

測試:

birthday = '2000-7-8'

getdate()= '2007-7-7'

算出結果為:6

(4)消除取值重復的行

[例]查詢選修了課程的學生學號

  1. select distinct Sno as 選修了課程的學生學號 from SC;
  2. select distinct Sno from SC;

 

(5)選擇表中若干元組(滿足條件的)

 

1.2、大小比較

    [例]查詢計算機系(IS)全體學生名單

select Sname as 學生姓名 from student where Sdept='IS';

 

    [例]查詢全體20歲以下的學生姓名和年齡

 

select Sname as 姓名,Sage as 年齡 from student where Sage<20;

1.3、確定范圍

 

    [例]查詢所有在20到23歲(含20和23)的學生姓名、系別和年齡

 

select Sname as 姓名,Sdept as 系別,Sage as 年齡 from student where Sage between20 and 23;

注意between 小數 and 大數。

 

1.4、in和not in確定集合

 

    [例]查詢IS系和CS系的全體學生姓名和性別

  1. select Sname as 姓名,Ssex as 性別 from student where Sdept='IS' or Sdept='CS';
  2. select Sname as 姓名,Ssex as 性別 from student where Sdept in ('IS','CS');

    [例]查詢既不屬於IS系,也不屬於MA系的學生姓名和年齡

  1. select Sname as 姓名,Sage as 年齡 from student where Sdept !='IS'and Sdept!='CS';
  2. select Sname as 姓名,Sage as 年齡 from student where Sdept not in('IS','MA');

1.5、字符匹配(like % _ )

 

    [例]查詢所有姓李的學生姓名和性別

select Sname as 姓名,Ssex as 性別 from student where Sname like '李%';

    [例]查詢所有“2002”年入學的學生學號、姓名和系別

select Sno as 學號,Sname as 姓名,Sdept as 系別 from student where Sno like'2002%';

    [例]查詢所有不姓“劉”的學生信息

select * from student where Sname not like'劉%';

    [例]查詢名稱含有“數據”的課程號、課程名及學分

select Cno as 課程號,Cname as 課程名,Ccredit as 學分 from course where Cname like '%數據%';

總結:

  1. select * from course where cname like '%數據%';包含數據的字符串
  2. select * from course where cname like '數據%';以數據開頭的字符串
  3. select * from course where cname like '%數據'; 以數據結尾的字符串

 

1.6、涉及空值的查詢(is null)

 

    [例]查詢沒有先修課的課程號和課程名

select Cno as 課程號,Cname as 課程名,Cpno from course where Cpno is null;

    [例]查詢所有有成績的學生學號、課程號及成績

select Sno as 學號,Cno as 課程號,Grade as 成績 from SC where Grade is not null;

 

1.7、查詢結果排序(order by )

 

[例]查詢選修了3號課程的學生學號和成績,結果按成績降序排列。

select Sno as 學號,Grade as 成績 from SC where Cno=3 order by Grade desc;

[例]查詢選修了3號課程的學生學號和成績,結果按成績升序排列。

select Sno as 學號,Grade as 成績 from SC where Cno=3 order by Grade asc;

1.8、聚集函數

count、sum、avg、max、min

[例]查詢學生總數

select count(*) as 學生總數 from student;

[例]查詢所有課程的總學分

select sum(Ccredit) as 所有課程總學分 from course;

[例]查詢全體學生平均年齡

select avg(Sage) as 平均年齡 from student;

[例]查詢1號課程的最高分

select max(Grade) as 1號課程的最高分 from SC where Cno=1;

1.9、分組統計(group by)

[例]查詢男女學生各有多少人。

select Ssex as 性別,count(*) as 人數 from student group by Ssex;

[例]查詢每個課程的課程號和平均分。

select Cno as 課程號,avg(Grade) as 平均分 from SC group by Cno;

【例】查詢選修了3門課程以上(含3門)的學生學號和選修課程數。

  1. select Sno as 學號 ,count(course.Cno) as 選修課程數
  2. From SC,course
  3. Where course.Cno=SC.Cno
  4. Group by Sno
  5. Having Count(course.Cno)>=3;

having 關鍵字后面直接跟聚集函數

在 SQL 中增加 HAVING 子句原因是,WHERE 關鍵字無法與合計函數一起使用。

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號課程),學生學號和選修課程數。

  1. select Sno as 學號 ,count(course.Cno) as 選修課程數
  2. From SC,course
  3. Where course.Cno=SC.Cno and course.Cno !=1
  4. Group by Sno
  5. Having Count(course.Cno)>=2;

【例】查詢不及格門數2門以上的學生學號。

  1. Select Sno
  2. from sc
  3. Where sc.Grade<60
  4. Group by Sno
  5. Having count(Cno)>=2;

 

【例】查詢有2名以上(含2名)學生選修了的課程號和選修人數。

  1. Select Cno,count(Sno)
  2. From SC
  3. Group by Cno
  4. Having count(sno)>=2

 

2、連接查詢

(1)等值與非等值連接查詢

[例]查詢每個學生及其的選修課程情況

  1. select student.Sno as 學號,course.Cno as 選修課號,SC.Grade as 成績
  2. from student,course,SC
  3. where student.Sno=SC.Sno and course.Cno=SC.Cno ;

 

(2)自身連接

[例]查詢每個學生的間接選修課

select SC.Sno as 學號,
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)外連接

[例]查詢所有學生選修課程情況(含沒選修課程的學生)

select student.Sno as 學號,
Sname as 姓名,
sc.Cno as 選修課程號
from student 
LEFT OUTER JOIN SC ON student.Sno=SC.Sno;

 join 用於根據兩個或多個表中的列之間的關系,從這些表中查詢數據

  1. JOIN: 如果表中有至少一個匹配,則返回行
  2. LEFT JOIN: 即使右表中沒有匹配,也從左表返回所有的行
  3. RIGHT JOIN: 即使左表中沒有匹配,也從右表返回所有的行
  4. FULL JOIN: 只要其中一個表中存在匹配,就返回行
  1. UNION 操作符用於合並兩個或多個 SELECT 語句的結果集。
  2. 請注意, UNION 內部的 SELECT 語句必須擁有相同數量的列。列也必須擁有相似的數據類型。同時,每條 SELECT 語句中的列的順序必須相同。

 

3 、嵌套查詢

(1)帶有IN謂詞的子查詢( 屬性 in (子查詢的查詢結果) )

【例】查詢與王敏同學在同一個系的學生信息。

  1. select *
  2. from student
  3. where Sdept in (
  4. select Sdept
  5. from student
  6. where Sname='王敏'
  7. );

 

【例】查詢不與王敏同學不在同一個系的學生信息。

  1. select *
  2. from student
  3. where Sdept not in (
  4. select Sdept
  5. from student
  6. whereSname= '王敏'
  7. );

【例】查詢選修了課程名是“信息系統”的學生學號和姓名。

  1. select student.Sno as 學號, Sname as 姓名
  2. from student,SC
  3. where student.Sno=SC.Sno and Cno in (
  4. select Cno
  5. from course
  6. where Cname='信息系統'
  7. )

 

【例】查詢曾與劉晨一同上課的學生學號和姓名。(假設:一個課程只有一個上課班)

  1. select distinct student.Sno as 學號, Sname as 姓名
  2. from student,SC
  3. where student.Sno=SC.Sno and Cno in (
  4. select Cno
  5. from SC,student
  6. where SC.Sno=student.Sno and student.Sno in (
  7. select Sno
  8. from student
  9. where student.Sname='劉晨'
  10. )
  11. )

 

  • 內層in 查出劉晨的學號sno,外層in查出劉晨所上課程的課程號。

(2)帶有比較運算符的子查詢(=,>=,<=,<>或!=)

【例】查詢與王敏同學在同一個系的所有學生信息  (=判斷)

  1. select *
  2. from student
  3. where Sdept=(
  4. select Sdept
  5. from student
  6. where Sname='王敏'
  7. )

 

【例】查詢每個學生超過該課程最低分的課程號。(同類課程不是最低分的),子查詢的結果返回一個數的時候,這個子查詢就可以當一個數用?可以使用in符號,或者大於小於符號。

  1. select Cno
  2. from SC a
  3. where Grade> (
  4. select min(Grade)
  5. from SC b
  6. where a.Cno=b.Cno
  7. )

 

【例】查詢每個學生超過他選修課程平均成績的課程號。

  1. select Cno
  2. from SC a
  3. where Grade> (
  4. select avg(Grade)
  5. from SC b
  6. where a.Sno=b.Sno
  7. )

 

(3)帶有ANY或ALL謂詞的子查詢

  • ANY表示任何一個,ALL表示所有,可以用在子查詢的括號前面

【例】查詢其他系中比計算機系某一學生年齡小的學生姓名,性別、年齡和所在系。

  1. select Sname as 姓名,Ssex as 性別, Sage as 年齡, Sdept as 所在系
  2. from student
  3. where Sage <(
  4. select Sage
  5. from student
  6. where Sdept='CS'
  7. );

 

【例】查詢其他系中比計算機系所有年齡都小的學生姓名和年齡。

  1. select Sname as 姓名, Sage as 年齡
  2. from student
  3. where Sdept<>'CS' and Sage <ALL (
  4. select Sage
  5. from student
  6. where Sdept='CS'
  7. );

 

(4 )帶有Exists謂詞的子查詢

【例】查詢所有選修了1號課程的學生姓名。

  1. select Sname as 姓名
  2. from student
  3. where Exists (
  4. select *
  5. from SC
  6. where Cno=1 and Sno=Student.Sno
  7. );

 

 

4、集合查詢

(1)並UNION

【例】 查詢計算機系的學生及年齡不大於19歲的學生詳細信息。

  1. select *
  2. from student
  3. where student.Sdept='CS'
  4. union
  5. select *
  6. from student
  7. where student.Sage<=19;

 

(2)交INTERSECT

【例】查詢選修了1號課程的與年齡不大於19歲的 學生 詳細信息 的交集。

  1. Select *
  2. from student,SC
  3. where student.Sno=SC.Sno and SC.Cno=1
  4. INTERSECT
  5. Select *
  6. from student
  7. where student.Sage<=19;

 

(3)差EXCEPT

【例】查詢計算機科學系的學生與年齡不大於19歲的學生詳細信息的差集。

  1. select *
  2. from student
  3. where student.Sdept='SC'
  4. EXCEPT
  5. select *
  6. from student
  7. where student.Sage<=19;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM