天天看點

mysql-資料查詢語句-單表

1、查詢指定列

select sno,sname from student;
           
mysql-資料查詢語句-單表

image.png

2、查詢全部列

select * from sc;
           
mysql-資料查詢語句-單表

3、查詢經過計算的值

select sname,2018-sage from student;  //目标列是表達式

select sname,'Year of birth:',2018-sage,lower(sdept) from student; //目标表達式可以是字元串常量、函數等

 select sname NAME,'Year of birth:' BIRTH, 2018-sage BIRTHDAY,lower(sdept) DEPARTMENT from student; //别名
           

select 子句的目标清單達式不僅可以是屬性列,還可以是表達式

mysql-資料查詢語句-單表

目标列 不僅可以是算術表達式,還可以是字元串常量、函數等。

mysql-資料查詢語句-單表

還可以通過指定别名來改變查詢結果的列标

mysql-資料查詢語句-單表

4、消除重複的行

select distinct sno from sc; //distinct 表示去重
select [all] sno from sc;  //如果沒有指定distinct,則預設是all
           
mysql-資料查詢語句-單表

條件查詢

5、比較大小

用于進行比較的運算符一般包括 =,>,<,>=,<=,!=,!>,!<。最後兩個分别是不大于,不小于。
//查詢計算機科學系全體學生的名單
select sname from student where sdept='CS';
           
mysql-資料查詢語句-單表
//查詢所有年齡在20歲以下的學生姓名及其年齡
select sname,sage from student where sage<20;
           
mysql-資料查詢語句-單表
//查詢考試成績不及格的學生的學号
select distinct sno from sc where grade<60;
           
mysql-資料查詢語句-單表

6、确定範圍

謂詞 between…and… 和 not between…and… 可以用來查找屬性值在(或不在)指定範圍内的無組,其中between後是範圍的下限(即低值),and 後是範圍的上限(即高值)。
//查詢年齡在20~30歲(包括20歲和30歲)之間的學生的姓名、系别和年齡。
select sname,sdept,sage from student where sage between 20 and 30;
           
mysql-資料查詢語句-單表
//查詢年齡不在20~30歲之間的學生姓名、系别和年齡。
 select sname,sdept,sage from student where sage not between 20 and 30;
           
mysql-資料查詢語句-單表

7、确定集合

謂詞 in 和 not in 可以用來查找屬性值屬于或不屬于指定集合的元組
//查詢計算機科學系(CS)、數學系(MA)和資訊系(IS)學生的姓名和性别。
 select sname,ssex from student where sdept in('CS','MA','IS');
           
mysql-資料查詢語句-單表
//查詢既不是計算機科學系(CS)、數學系(MA),也不是資訊系(IS)學生的姓名和性别。
select sname,ssex from student where sdept not in('CS','MA','IS');
           
mysql-資料查詢語句-單表

8、字元比對

謂詞 like 可以用來進行字元串的比對。%(百分号)代表任意長度(長度可以為0)的字元串。_(下劃線)代表任意單個字元。
//查詢學号為201215121的學生的詳細情況
select * from student where sno like '201215121';
//上式可以等價為
select * from student where sno='201215121';
           

如果 like 後面的比對串不含通配符,則可以用 = (等于)運算符取代 like 謂詞,用 != 或 <>(不等于)運算符取代not like 謂詞。

mysql-資料查詢語句-單表
//查詢所有姓劉的學生的姓名、學号和性别
select sname,sno,ssex from student where sname like '劉%'; //%代表任意長度
           
mysql-資料查詢語句-單表
//查詢姓“歐陽”且全名為三個漢字的學生的姓名。
select sname,sno from student where sname like '歐陽_'; 
//    _ 一般用于查詢明确長度的字元比對
           
mysql-資料查詢語句-單表
//查詢名字中第二個字為“陽”的學生的姓名和學号
 select sname,sno from student where sname like '_陽%';
           
mysql-資料查詢語句-單表
//查詢所有不姓劉的學生的姓名、學号和性别
select sname,sno,ssex from student where sname not like '劉%';
           
mysql-資料查詢語句-單表
//查詢DB_Design課程的課程号和學分
select cno,ccredit from course where cname like 'DB\\_Design';
           

如果條件裡面包含一些特殊的字元,那麼就要進行轉義了,在mysql裡面是在特殊字元前面加 \(反斜杠)進行轉義。

mysql-資料查詢語句-單表
//查詢以“DB_開頭,且倒數第三個字元為 i 的課程的詳細情況”
select * from course where cname like 'DB\_%i__'; 
           
mysql-資料查詢語句-單表

9、涉及空值的查詢

//查詢缺少成績的學生的學号和相應的課程号
select sno,cno from sc where grade is null; 
//注意:這裡的 “is” 不能用等号(=)來代替
           
mysql-資料查詢語句-單表
//查詢所有有成績的學生學号和課程号
select sno,cno
    -> from sc
    -> where grade is not null;
           
mysql-資料查詢語句-單表

10、多重條件查詢

邏輯運算符 and 和 or 可用業連接配接多個查詢條件。and 的優先級高于 or , 但是可以用括号改變優先級。
//查詢計算機科學系(CS)、數學系(MA)和資訊系(IS)學生的姓名和性别
mysql> select sname, ssex from student
    -> where sdept='CS' OR sdept='MA' or sdept='IS';
// in 謂詞實際上是多個or運算符的縮寫
           
mysql-資料查詢語句-單表
//查詢計算機科學第年齡在20歲以下的學生姓名
mysql> select sname
    -> from student
    -> where sdept='CS' and sage<20;
           
mysql-資料查詢語句-單表

10、order by子句

用order by 子句對查詢結果一個或多個屬性列的升序(asc)或降序(desc)排列,預設值為升序。
//查詢選修了3号課程的學生的學号及其成績,查詢結果按分數的降序排列。
mysql> select sno,grade
    -> from sc
    -> where cno='3'
    -> order by grade desc;
           
mysql-資料查詢語句-單表
查詢全體學生情況,查詢結果按所在系的系号升序排列,同一系的學生按年齡降序排列
mysql> select *
    -> from student
    -> order by sdept,sage desc;
           
mysql-資料查詢語句-單表

11、聚集函數

//查詢學生總人數
mysql> select count(*)
    -> from student;
           
mysql-資料查詢語句-單表
//查詢選修了課程的學生人數
mysql> select count(distinct sno)
    -> from sc;
           
mysql-資料查詢語句-單表
//計算選修1号課程的學生平均成績
mysql> select avg(grade)
    -> from sc
    -> where cno='1';
           
mysql-資料查詢語句-單表
//查詢選修1号課程的學生最高分
mysql> select max(grade)
    -> from sc
    -> where cno='1';
           
mysql-資料查詢語句-單表
//查詢學生201215012選修課程的總學分數
mysql> select sum(ccredit)
    -> from sc,course
    -> where sno='201215012' and sc.cno=course.cno;
           
mysql-資料查詢語句-單表

當聚集函數遇到空值時,除 count() 外,都跳過空值而隻處理非空值。count() 是對元組進行計數,某個元組的一個部分列取空值不影響count的統計結果

注意: where 子句中是不能用聚合函數作為條件表達式的。聚集函數隻能用于select 子句和 group by 中的 having 子句。

12、group by 子句

group by 子句将查詢結果按某一列或多列的值分組,值相等的為一組。分組後聚集函數将作用于每一個組,即每一組都有一個函數值。
//求各個課程号及相應的選課人數
mysql> select cno,count(sno)
    -> from sc
    -> group by cno;
           
mysql-資料查詢語句-單表

如果分組後還要求按一定的條件對這些級進行篩選,最終隻輸出滿足指定條件的組,則可以使用having短語指定篩選條件。

//查詢選修了三門以上課程的學生學号
mysql> select sno
    -> from sc
    -> group by sno
    -> having count(*)>3;
           
mysql-資料查詢語句-單表

where 子句與 having 短語的差別在于作用對象不同。where 子句作用于基本表或視圖,從中選擇滿足條件的元組。having 短語作用于組,從中選擇滿足條件的組。

//查詢平均成績大于等于90分的學生學号和平均成績
mysql> select sno,avg(grade)
    -> from sc
    -> group by sno
    -> having avg(grade)>90;
           
mysql-資料查詢語句-單表