1. 程式人生 > 其它 >SQL 資料庫高階子查詢

SQL 資料庫高階子查詢

--分組的關鍵字
--group by

--where和having的區別
--where:分組前的條件查詢
--having:分組後的過濾查詢

--增刪改查關鍵字
--insert into
--delete
--update set
--select

--建立表和刪除表的關鍵字
--create、drop

--統計五個函式
--count()統計總數
--max()最大
--min()最小
--avg()平均
--sum()總和

--聯表查詢
/*
select * from 表1 別名
[inner]ioin 表2 別名
on 聯表查詢
*/
--內聯查詢
/*
inner join
*/
--外聯查詢
/*
左、右、全

左:left join
右:right join
全:full join
*/

select * from Student
--查詢年齡比蔣歡大的學生資訊

--查詢到蔣歡的年齡
select * from Student where sage>(
select sage from Student where sname='蔣歡'
)
--查詢大於平均年齡的學生資訊
select * from Student where sage>(
--先查詢出平均年齡
select avg(sage)from Student

)

--in:等於or 在指定值內
select * from score
--查詢參加考試的學生資訊
select * from Student where sid in(
--查詢到有成績的學生學號
select sid from score
)
--查詢學號為1的學生資訊
select * from Student where sid = 1
--查詢學號1和2的學生資訊
select * from Student where sid=1or sid =2
--查詢學號1、2、3、4、5的學生資訊
select * from Student where sid in(1,2,3,4,5)

--not in:不包括
--查詢沒有考試的學生資訊
select * from Student where sid not in(
select sid from score
)
select * from score