欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL常用SQL語(yǔ)句總結(jié)包含復(fù)雜SQL查詢

 更新時(shí)間:2019年02月25日 10:01:48   作者:琦彥  
今天小編就為大家分享一篇關(guān)于MySQL常用SQL語(yǔ)句總結(jié)包含復(fù)雜SQL查詢,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧

1、復(fù)雜SQL查詢

1.1、單表查詢

(1)選擇指定的列

[例]查詢?nèi)w學(xué)生的學(xué)號(hào)和姓名

select Sno as 學(xué)號(hào),Sname as 姓名 from student;
select Sno,Sname from student;

(2)查詢?nèi)苛?/p>

[例]查詢?nèi)w學(xué)生的詳細(xì)信息

select * from student;

(3)對(duì)查詢后的指定列進(jìn)行命名

[例]查詢?nèi)繉W(xué)生的“姓名”及其“出生年”兩列

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

(4)消除取值重復(fù)的行

[例]查詢選修了課程的學(xué)生學(xué)號(hào)

select distinct Sno as 選修了課程的學(xué)生學(xué)號(hào) from SC;
select distinct Sno from SC;

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

1.2、大小比較

[例]查詢計(jì)算機(jī)系(IS)全體學(xué)生名單

select Sname as 學(xué)生姓名 from student where Sdept='IS';

[例]查詢?nèi)w20歲以下的學(xué)生姓名和年齡

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

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é)號(hào)、姓名和系別

select Sno as 學(xué)號(hào),Sname as 姓名,Sdept as 系別 from student where Sno like'2002%';

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

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

[例]查詢名稱含有“數(shù)據(jù)”的課程號(hào)、課程名及學(xué)分

select Cno as 課程號(hào),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ù)開(kāi)頭的字符串
select * from course where cname like '%數(shù)據(jù)'; 以數(shù)據(jù)結(jié)尾的字符串

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

[例]查詢沒(méi)有先修課的課程號(hào)和課程名

select Cno as 課程號(hào),Cname as 課程名,Cpno from course where Cpno is null;

[例]查詢所有有成績(jī)的學(xué)生學(xué)號(hào)、課程號(hào)及成績(jī)

select Sno as 學(xué)號(hào),Cno as 課程號(hào),Grade as 成績(jī) from SC where Grade is not null;

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

[例]查詢選修了3號(hào)課程的學(xué)生學(xué)號(hào)和成績(jī),結(jié)果按成績(jī)降序排列。

select Sno as 學(xué)號(hào),Grade as 成績(jī) from SC where Cno=3 order by Grade desc;

[例]查詢選修了3號(hào)課程的學(xué)生學(xué)號(hào)和成績(jī),結(jié)果按成績(jī)升序排列。

select Sno as 學(xué)號(hào),Grade as 成績(jī) 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號(hào)課程的最高分

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

1.9、分組統(tǒng)計(jì)(group by)

[例]查詢男女學(xué)生各有多少人。

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

[例]查詢每個(gè)課程的課程號(hào)和平均分。

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

【例】查詢選修了3門課程以上(含3門)的學(xué)生學(xué)號(hào)和選修課程數(shù)。

select Sno as 學(xué)號(hào) ,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)鍵字無(wú)法與合計(jì)函數(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號(hào)課程),學(xué)生學(xué)號(hào)和選修課程數(shù)。

select Sno as 學(xué)號(hào) ,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é)號(hào)。

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

【例】查詢有2名以上(含2名)學(xué)生選修了的課程號(hào)和選修人數(shù)。

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

2、連接查詢

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

[例]查詢每個(gè)學(xué)生及其的選修課程情況

select student.Sno as 學(xué)號(hào),course.Cno as 選修課號(hào),SC.Grade as 成績(jī) 
from student,course,SC 
where student.Sno=SC.Sno and course.Cno=SC.Cno ;

(2)自身連接

[例]查詢每個(gè)學(xué)生的間接選修課

select SC.Sno as 學(xué)號(hào),
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é)生選修課程情況(含沒(méi)選修課程的學(xué)生)

select student.Sno as 學(xué)號(hào),
Sname as 姓名,
sc.Cno as 選修課程號(hào)
from student 
LEFT OUTER JOIN SC ON student.Sno=SC.Sno;

join 用于根據(jù)兩個(gè)或多個(gè)表中的列之間的關(guān)系,從這些表中查詢數(shù)據(jù)

JOIN: 如果表中有至少一個(gè)匹配,則返回行
LEFT JOIN: 即使右表中沒(méi)有匹配,也從左表返回所有的行
RIGHT JOIN: 即使左表中沒(méi)有匹配,也從右表返回所有的行
FULL JOIN: 只要其中一個(gè)表中存在匹配,就返回行
UNION 操作符用于合并兩個(gè)或多個(gè) SELECT 語(yǔ)句的結(jié)果集。
請(qǐng)注意,UNION 內(nèi)部的 SELECT 語(yǔ)句必須擁有相同數(shù)量的列。列也必須擁有相似的數(shù)據(jù)類型。同時(shí),每條 SELECT 語(yǔ)句中的列的順序必須相同。

3 、嵌套查詢

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

【例】查詢與王敏同學(xué)在同一個(gè)系的學(xué)生信息。

select *
from student
where Sdept in (
 select Sdept
 from student
 where Sname='王敏'
);

【例】查詢不與王敏同學(xué)不在同一個(gè)系的學(xué)生信息。

select *
from student
where Sdept not in (
 select Sdept
 from student
 whereSname='王敏'
);

【例】查詢選修了課程名是“信息系統(tǒng)”的學(xué)生學(xué)號(hào)和姓名。

select student.Sno as 學(xué)號(hào), Sname as 姓名
from student,SC
where student.Sno=SC.Sno and Cno in (
 select Cno
 from course
 where Cname='信息系統(tǒng)'
)

【例】查詢?cè)c劉晨一同上課的學(xué)生學(xué)號(hào)和姓名。(假設(shè):一個(gè)課程只有一個(gè)上課班)

select distinct student.Sno as 學(xué)號(hào), 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é)號(hào)sno,外層in查出劉晨所上課程的課程號(hào)。

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

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

select *
from student
where Sdept=(
 select Sdept
 from student
 where Sname='王敏'
)

【例】查詢每個(gè)學(xué)生超過(guò)該課程最低分的課程號(hào)。(同類課程不是最低分的),子查詢的結(jié)果返回一個(gè)數(shù)的時(shí)候,這個(gè)子查詢就可以當(dāng)一個(gè)數(shù)用?可以使用in符號(hào),或者大于小于符號(hào)。

select Cno
from SC a
where Grade> (
 select min(Grade)
 from SC b
 where a.Cno=b.Cno
)

【例】查詢每個(gè)學(xué)生超過(guò)他選修課程平均成績(jī)的課程號(hào)。

select Cno
from SC a
where Grade> (
 select avg(Grade)
 from SC b
 where a.Sno=b.Sno
)

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

  • ANY表示任何一個(gè),ALL表示所有,可以用在子查詢的括號(hào)前面

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

select Sname as 姓名,Ssex as 性別, Sage as 年齡, Sdept as 所在系
from student
where Sage <(
 select Sage
 from student
 where Sdept='CS'
);

【例】查詢其他系中比計(jì)算機(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號(hào)課程的學(xué)生姓名。

select Sname as 姓名
from student
where Exists (
 select *
 from SC
 where Cno=1 and Sno=Student.Sno
);

4、集合查詢

(1)并UNION

【例】 查詢計(jì)算機(jī)系的學(xué)生及年齡不大于19歲的學(xué)生詳細(xì)信息。

select *
from student
where student.Sdept='CS'
union
select *
from student
where student.Sage<=19;

(2)交INTERSECT

【例】查詢選修了1號(hào)課程的與年齡不大于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ì)算機(jī)科學(xué)系的學(xué)生與年齡不大于19歲的學(xué)生詳細(xì)信息的差集。

select *
from student
where student.Sdept='SC'
EXCEPT
select *
from student
where student.Sage<=19;

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

  • mysql 5.7.27 安裝配置方法圖文教程

    mysql 5.7.27 安裝配置方法圖文教程

    這篇文章主要為大家詳細(xì)介紹了mysql 5.7.27 安裝配置方法圖文教程,文中安裝步驟介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • MySQL-8.0.26配置圖文教程

    MySQL-8.0.26配置圖文教程

    最近公司項(xiàng)目更換數(shù)據(jù)庫(kù)版本,在此記錄分享一下自己安裝配置MySQL8.0版本的過(guò)程吧,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)MySQL-8.0.26配置教程感興趣的朋友跟隨小編一起看看吧
    2021-12-12
  • Mysql數(shù)據(jù)庫(kù)之索引優(yōu)化

    Mysql數(shù)據(jù)庫(kù)之索引優(yōu)化

    MySQL憑借著出色的性能、低廉的成本、豐富的資源,已經(jīng)成為絕大多數(shù)互聯(lián)網(wǎng)公司的首選關(guān)系型數(shù)據(jù)庫(kù)。本文給大家介紹mysql數(shù)據(jù)庫(kù)之索引優(yōu)化,感興趣的朋友一起學(xué)習(xí)吧
    2016-03-03
  • 在MySQL中如何存取List<String>數(shù)據(jù)

    在MySQL中如何存取List<String>數(shù)據(jù)

    這篇文章主要介紹了在MySQL中如何存取List<String>數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 使用mysql查詢當(dāng)天、近一周、近一個(gè)月及近一年的數(shù)據(jù)

    使用mysql查詢當(dāng)天、近一周、近一個(gè)月及近一年的數(shù)據(jù)

    不論你是一名數(shù)據(jù)庫(kù)管理員或SQL開(kāi)發(fā)者,還是一名簡(jiǎn)單的MySQL用戶,掌握查詢特定日期數(shù)據(jù)的方法都是必不可少的,下面這篇文章主要給大家介紹了關(guān)于如何使用mysql查詢當(dāng)天、近一周、近一個(gè)月及近一年的數(shù)據(jù),需要的朋友可以參考下
    2023-06-06
  • MySQL運(yùn)算符!=和<>及=和<=>的使用區(qū)別

    MySQL運(yùn)算符!=和<>及=和<=>的使用區(qū)別

    本文主要介紹了MySQL運(yùn)算符!=和<>及=和<=>的使用區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • mysql Access denied for user ‘root’@’localhost’ (using password: YES)解決方法

    mysql Access denied for user ‘root’@’localhost’ (using passw

    這篇文章主要介紹了mysql Access denied for user ‘root’@’localhost’ (using password: YES)解決方法,本文給出詳細(xì)的解決步驟及操作注釋,需要的朋友可以參考下
    2015-07-07
  • cmd連接mysql的方法詳解

    cmd連接mysql的方法詳解

    本篇文章是對(duì)cmd連接mysql的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • win11設(shè)置mysql開(kāi)機(jī)自啟的實(shí)現(xiàn)方法

    win11設(shè)置mysql開(kāi)機(jī)自啟的實(shí)現(xiàn)方法

    本文主要介紹了win11設(shè)置mysql開(kāi)機(jī)自啟的實(shí)現(xiàn)方法,要通過(guò)命令行方式設(shè)置,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03
  • Mysql時(shí)間軸數(shù)據(jù) 獲取同一天數(shù)據(jù)的前三條

    Mysql時(shí)間軸數(shù)據(jù) 獲取同一天數(shù)據(jù)的前三條

    這篇文章主要介紹了Mysql時(shí)間軸數(shù)據(jù) 獲取同一天數(shù)據(jù)的前三條 ,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07

最新評(píng)論