1簡單查詢 select* from 表名
select name as“姓名”fromstu (把name改為名字)
2條件查詢 where 后面跟條件 條件要寫清楚
3模糊查詢 like no like %代表任意多個字符 _代表一個字符
4排序查詢 order by 字段 排序值 (desc降 asc升)
5范圍查詢 between....and...
6離散查詢 in notin
7聚合查詢 sun 求和 count數據條數 max最大值 min最小值 avg平均值
8分頁查詢 limit 從第幾條開始,取多少數據
表名limit(pagesize-1)*5,5 pagesize是頁數 *5 是每頁條數 ,5 取多少條
9去重查詢 distinct select distinct 表段名 from 表名
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
高級查詢
- 連接查詢,對列的擴展
Select * from student as stu,score as sc
where stu.sno = sc.sno and sc.sno = “103” ;
2.聯合查詢,對行的擴展
select Code,Name from Info
union
select Code,Name from Nation
3.子查詢
(1)無關子查詢
外層查詢 (里層查詢)
子查詢的結果當做父查詢的條件
子查詢:select Code from Nation where Name='漢族'
父查詢:select * from Info where Nation = ''
select * from Info where Nation = (select Code from Nation where Name='漢族')
(2)相關子查詢
查詢汽車表中油耗低於該系列平均油耗的所有汽車信息
父查詢:select * from Car where Oil<(該系列平均油耗)
子查詢:select avg(Oil) from Car where Brand = '某個系列'
select * from Car a where Oil<(select avg(Oil) from Car b where b.Brand = a.Brand )