1. 程式人生 > >實訓第四天

實訓第四天

顯示 合並 聚合函數 esc car like 最小 cno info

實訓第四天了,最近腦子裏灌的東西比較多,現在終於感到腦子不夠用了,腦子疼啊。

今天學的依舊是mysql,學的是查詢,查詢命令單個簡答,兩個條件三個條件一組合,哎呀我的腦容量直接感覺不夠用了,差點哭暈在廁所

查詢條件

(1)簡單查詢

select * from Info

select Code as ‘代號‘,Name as ‘姓名‘ from Info as a

(2) 條件查詢

Where後面跟條件 條件要寫清楚

查詢成績表中成績(degree)為92的信息

Select * from score where degree =”92”;

查詢成績表中課程號是3-245並且成績是86的信息

Select * from score where cno=‘3-245‘ and degree=”86”

或者用or 並且用and

(3) 模糊查詢 like not like

查找老師表中姓李的 名字是兩個字老師

select * from teacher

where tName like ‘%%‘

%代表任意多個字符 _代表一個字符

(4)排序查詢 order by 字段 排序值(desc降序/asc升序 )

select * from student order by class asc

(5)範圍查詢 關系運算符 between。。。and

select * from Car where Price>=40 and Price<=60

select * from Car where Price between 40 and 50

(6)離散查詢 in not in

select * from student where sname in (‘張三‘,‘李四‘)

。。。where sname =“張三” or sname =“李四”

(7)聚合函數,統計查詢

select sum(Price) from Car #查詢所有價格之和 sum()求和

select count(Code) from Car #查詢數據條數

select max(Code) from Car #求最大值

select min(Brand) from Car #求最小值

select avg(Price) from Car #求平均值

(8)分頁查詢 limit 從第幾條開始,取多少條數據

#每頁顯示5條數據,取第2頁的數據

select * from student limit (pageSize-1)*5,5

註:Where 後面不能接limit,表示為空

select * from student limit (n-1)*5,5

select * from student limit 5 只寫一個5時,表示默認從第0條開始

(9)去重查詢distinct

select distinct cno from score;

(10)分組查詢 group by 字段 having 條件

select count(*),cno,group_concat(degree),sum(degree) from score group by cno ;

select cno,group_concat(degree),sum(degree) from score group by cno having count(*)>3

#分組之後根據條件查詢使用having 不使用where

根據某個字段分組

Having相當於where

Select count(*),cno,group_concat(sno)

高級查詢

  1. 連接查詢,對列的擴展

Select * from student as stu,score as sc

where stu.sno = sc.sno and sc.sno = “103” ;

連接查詢Join 表名on條件

Select * from teacher

Join course

On+條件(on teacher.tno = course.tno

Join score

On(on course.cno = score.cno)

2.聯合查詢,對行的擴展

select Code,Name from Info

union

select Code,Name from Nation

不相同條件不能連接,必須的一一對應的查詢條件

3.子查詢

(1)無關子查詢

查詢老師的信息,根據老師的編號

Select * from teacher where tno =?

查詢教計算機的老師的編號

Select tno from course where cname = “計算機導論”;

合並

select * fron teacher where tno =Select tno from course where cname = “計算機導論”

;

以上是所學內容,還是需要多練啊

實訓第四天