您好,登錄后才能下訂單哦!
MySQL中有哪些常用的SQL語句,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
1、復雜SQL查詢
1.1、單表查詢
(1)選擇指定的列
[例]查詢全體學生的學號和姓名
select Sno as 學號,Sname as 姓名 from student; select Sno,Sname from student;
(2)查詢全部列
[例]查詢全體學生的詳細信息
select * from student;
(3)對查詢后的指定列進行命名
[例]查詢全部學生的“姓名”及其“出生年”兩列
select Sname as 姓名,(2014-Sage) as 出生年 from student; select Sname ,(2014-Sage) from student;
(4)消除取值重復的行
[例]查詢選修了課程的學生學號
select distinct Sno as 選修了課程的學生學號 from SC; 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 小數(shù) and 大數(shù)。
1.4、in和not in確定集合
[例]查詢IS系和CS系的全體學生姓名和性別
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系的學生姓名和年齡
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 % _ )
[例]查詢所有姓李的學生姓名和性別
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'劉%';
[例]查詢名稱含有“數(shù)據(jù)”的課程號、課程名及學分
select Cno as 課程號,Cname as 課程名,Ccredit as 學分 from course where Cname like '%數(shù)據(jù)%';
總結:
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ù)結尾的字符串
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、聚集函數(shù)
count、sum、avg、max、min
[例]查詢學生總數(shù)
select count(*) as 學生總數(shù) 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、分組統(tǒng)計(group by)
[例]查詢男女學生各有多少人。
select Ssex as 性別,count(*) as 人數(shù) from student group by Ssex;
[例]查詢每個課程的課程號和平均分。
select Cno as 課程號,avg(Grade) as 平均分 from SC group by Cno;
【例】查詢選修了3門課程以上(含3門)的學生學號和選修課程數(shù)。
select Sno as 學號 ,count(course.Cno) as 選修課程數(shù) From SC,course Where course.Cno=SC.Cno Group by Sno Having Count(course.Cno)>=3;
having 關鍵字后面直接跟聚集函數(shù)
在 SQL 中增加 HAVING 子句原因是,WHERE 關鍵字無法與合計函數(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號課程),學生學號和選修課程數(shù)。
select Sno as 學號 ,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門以上的學生學號。
Select Sno from sc Where sc.Grade<60 Group by Sno Having count(Cno)>=2;
【例】查詢有2名以上(含2名)學生選修了的課程號和選修人數(shù)。
Select Cno,count(Sno) From SC Group by Cno Having count(sno)>=2
2、連接查詢
(1)等值與非等值連接查詢
[例]查詢每個學生及其的選修課程情況
select student.Sno as 學號,course.Cno as 選修課號,SC.Grade as 成績 from student,course,SC 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 用于根據(jù)兩個或多個表中的列之間的關系,從這些表中查詢數(shù)據(jù)
JOIN: 如果表中有至少一個匹配,則返回行 LEFT JOIN: 即使右表中沒有匹配,也從左表返回所有的行 RIGHT JOIN: 即使左表中沒有匹配,也從右表返回所有的行 FULL JOIN: 只要其中一個表中存在匹配,就返回行
UNION 操作符用于合并兩個或多個 SELECT 語句的結果集。 請注意,UNION 內部的 SELECT 語句必須擁有相同數(shù)量的列。列也必須擁有相似的數(shù)據(jù)類型。同時,每條 SELECT 語句中的列的順序必須相同。
3 、嵌套查詢
(1)帶有IN謂詞的子查詢( 屬性 in (子查詢的查詢結果) )
【例】查詢與王敏同學在同一個系的學生信息。
select * from student where Sdept in ( select Sdept from student where Sname='王敏' );
【例】查詢不與王敏同學不在同一個系的學生信息。
select * from student where Sdept not in ( select Sdept from student whereSname='王敏' );
【例】查詢選修了課程名是“信息系統(tǒng)”的學生學號和姓名。
select student.Sno as 學號, Sname as 姓名 from student,SC where student.Sno=SC.Sno and Cno in ( select Cno from course where Cname='信息系統(tǒng)' )
【例】查詢曾與劉晨一同上課的學生學號和姓名。(假設:一個課程只有一個上課班)
select distinct student.Sno as 學號, 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='劉晨' ) )
內層in 查出劉晨的學號sno,外層in查出劉晨所上課程的課程號。
(2)帶有比較運算符的子查詢(=,>=,<=,<>或!=)
【例】查詢與王敏同學在同一個系的所有學生信息 (=判斷)
select * from student where Sdept=( select Sdept from student where Sname='王敏' )
【例】查詢每個學生超過該課程最低分的課程號。(同類課程不是最低分的),子查詢的結果返回一個數(shù)的時候,這個子查詢就可以當一個數(shù)用?可以使用in符號,或者大于小于符號。
select Cno from SC a where Grade> ( select min(Grade) from SC b where a.Cno=b.Cno )
【例】查詢每個學生超過他選修課程平均成績的課程號。
select Cno from SC a where Grade> ( select avg(Grade) from SC b where a.Sno=b.Sno )
(3)帶有ANY或ALL謂詞的子查詢
ANY表示任何一個,ALL表示所有,可以用在子查詢的括號前面
【例】查詢其他系中比計算機系某一學生年齡小的學生姓名,性別、年齡和所在系。
select Sname as 姓名,Ssex as 性別, Sage as 年齡, Sdept as 所在系 from student where Sage <( select Sage from student where Sdept='CS' );
【例】查詢其他系中比計算機系所有年齡都小的學生姓名和年齡。
select Sname as 姓名, Sage as 年齡 from student where Sdept<>'CS' and Sage <ALL ( select Sage from student where Sdept='CS' );
(4 )帶有Exists謂詞的子查詢
【例】查詢所有選修了1號課程的學生姓名。
select Sname as 姓名 from student where Exists ( select * from SC where Cno=1 and Sno=Student.Sno );
4、集合查詢
(1)并UNION
【例】 查詢計算機系的學生及年齡不大于19歲的學生詳細信息。
select * from student where student.Sdept='CS' union select * from student where student.Sage<=19;
(2)交INTERSECT
【例】查詢選修了1號課程的與年齡不大于19歲的 學生 詳細信息 的交集。
Select * from student,SC where student.Sno=SC.Sno and SC.Cno=1 INTERSECT Select * from student where student.Sage<=19;
(3)差EXCEPT
【例】查詢計算機科學系的學生與年齡不大于19歲的學生詳細信息的差集。
select * from student where student.Sdept='SC' EXCEPT select * from student where student.Sage<=19;
看完上述內容,你們掌握MySQL中有哪些常用的SQL語句的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。