23. Khoa HTTT - 畉i h畛c CNTT 23
5.2 Truy v畉n 董n gi畉n(1)
SELECT
T動董ng 動董ng ph辿p chi畉u c畛a SQH
Li畛t k棚 c叩c thu畛c t鱈nh c畉n hi畛n th畛 trong k畉t qu畉
WHERE
T動董ng 畛ng v畛i i畛u ki畛n ch畛n trong SQH
i畛u ki畛n li棚n quan t畛i thu畛c t鱈nh, s畛 d畛ng c叩c ph辿p n畛i
lu畉n l箪 AND, OR, NOT, c叩c ph辿p to叩n so s叩nh,
BETWEEN
FROM
Li畛t k棚 c叩c quan h畛 c畉n thi畉t, c叩c ph辿p k畉t
24. Khoa HTTT - 畉i h畛c CNTT 24
5.2 Truy v畉n 董n gi畉n(2)
T狸m masp, tensp do Trung Quoc s畉n xu畉t
c坦 gi叩 t畛 20000 畉n 30000
Select masp,tensp
From SANPHAM
Where nuocsx=Trung Quoc
and gia between 20000 and 30000
25. Khoa HTTT - 畉i h畛c CNTT 25
5.3 Ph辿p k畉t(1)
Inner Join, Left Join, Right Join, Full Join
V鱈 d畛:
In ra danh s叩ch c叩c kh叩ch hng (MAKH,
HOTEN) 達 mua hng trong ngy 1/1/2007.
select KHACHHANG.makh,hoten
from KHACHHANG inner join HOADON on
KHACHHANG.makh=HOADON.makh
where nghd='1/1/2007'
26. Khoa HTTT - 畉i h畛c CNTT 26
5.3 Ph辿p k畉t (2)
V鱈 d畛: In ra danh s叩ch t畉t c畉 c叩c h坦a 董n v h畛 t棚n
c畛a kh叩ch hng mua h坦a 董n 坦 (n畉u c坦)
Select sohd, hoten
From HOADON left join KHACHHANG on
HOADON.makh=KHACHHANG.makh
Select sohd, hoten
From HOADON ,KHACHHANG
where HOADON.makh*=KHACHHANG.makh
27. Khoa HTTT - 畉i h畛c CNTT 27
5.4 畉t b鱈 danh, s畛 d畛ng *, distinct
畉t b鱈 danh Alias: cho thu畛c t鱈nh v quan h畛:
t棚n_c滴 AS t棚n_m畛i
Select manv,hoten as [ho va ten] From NHANVIEN
Li畛t k棚 t畉t c畉 c叩c thu畛c t鱈nh c畛a quan h畛:
Select * from Nhanvien
Select NHANVIEN.* from NHANVIEN
Distinct: tr湛ng ch畛 l畉y m畛t l畉n
Select distinct nuocsx from SANPHAM
S畉p x畉p k畉t qu畉 hi畛n th畛: Order by
Select * from SANPHAM order by nuocsx, gia DESC
28. Khoa HTTT - 畉i h畛c CNTT 28
5.5 To叩n t畛 truy v畉n(1)
To叩n t畛 so s叩nh: =, >,<,>=,<=,<>
To叩n t畛 logic: AND, OR, NOT
Ph辿p to叩n: +, - ,* , /
BETWEEN . AND
IS NULL, IS NOT NULL
LIKE (_ %)
IN, NOT IN
EXISTS , NOT EXISTS
SOME, ALL
29. Khoa HTTT - 畉i h畛c CNTT 29
5.5 To叩n t畛 truy v畉n(2)
IS NULL, IS NOT NULL
Select sohd from HOADON where makh is Null
Select * from HOADON where makh is Not Null
To叩n t畛 so s叩nh, ph辿p to叩n
Select gia*1.1 as [gia ban] from SANPHAM where
nuocsx<>Viet Nam
Select * from SANPHAM where (gia between 20000 and
30000) OR (nuocsx=Viet Nam)
To叩n t畛 IN, NOT IN
Select * from SANPHAM where masp NOT IN
(BB01,BB02,BB03)
30. Khoa HTTT - 畉i h畛c CNTT 30
To叩n t畛 LIKE
So s叩nh chu畛i t動董ng 畛i
C炭 ph叩p: s LIKE p, p c坦 th畛 ch畛a % ho畉c _
% : thay th畉 m畛t chu畛i k箪 t畛 b畉t k畛
_ : thay th畉 m畛t k箪 t畛 b畉t k畛
V鱈 d畛: Select masp,tensp from SANPHAM
where masp like 'B%01
5.5 To叩n t畛 so s叩nh(3)
31. Khoa HTTT - 畉i h畛c CNTT 31
5.6 C但u truy v畉n con (1)
In ho畉c Exists
V鱈 d畛: T狸m c叩c s畛 h坦a 董n mua c湛ng l炭c 2 s畉n ph畉m
c坦 m達 s畛 BB01 v BB02.
select distinct sohd
from CTHD where masp='BB01' and sohd IN
(select distinct sohd from CTHD where masp='BB02')
select distinct A.sohd
from CTHD A where A.masp='BB01' and
EXISTS (select * from CTHD B
where B.masp='BB02 and A.sohd=B.sohd)
32. Khoa HTTT - 畉i h畛c CNTT 32
5.6 C但u truy v畉n con (2)
Not In ho畉c Not Exists
V鱈 d畛: T狸m c叩c s畛 h坦a 董n c坦 mua s畉n ph畉m m達 s畛
BB01 nh動ng kh担ng mua s畉n ph畉m m達 s畛 BB02.
select distinct sohd
from CTHD where masp='BB01' and sohd NOT IN
(select distinct sohd from CTHD where masp='BB02')
select distinct A.sohd
from CTHD A where A.masp='BB01' and
NOT EXists (select * from CTHD B
where B.masp='BB02 and A.sohd=B.sohd)
33. Khoa HTTT - 畉i h畛c CNTT 33
5.7 Ph辿p chia
S畛 d畛ng NOT EXISTS
V鱈 d畛: T狸m s畛 h坦a 董n 達 mua t畉t c畉 nh畛ng s畉n
ph畉m do Trung Quoc s畉n xu畉t.
Select sohd from HOADON where not exists
(select * from SANPHAM
where nuocsx=Trung Quoc and not exists
(select * from CTHD where
HOADON.sohd=CTHD.sohd and
CTHD.masp=SANPHAM.masp))