UNION ifodasidan foydalanish.
UNION ifodasi bir yoki bir necha SQL so‘rovlar nitijasini birlashtirishga imkon beradi.
Misol: Londonga joylashgan xamma sotuvchilar va buyurtmachilarni bitta jadvalda chiqaring.
SELECT SNum, SName FROM Salepeople
WHERE City = 'London'
UNION
SELECT CNum, CName FROM Customers
WHERE City = 'London';
Misol: O‘z shaxarlarida buyurtmachilarga ega yoki ega emasligini ko‘rsatgan xolda xamma
sotuvchilarni chiqarish.
SELECT Salepeople.SNum, SName, CName, Comm FROM Salepeople, Customers
WHERE Salepeople.City = Customers.City
UNION
SELECT SNum, SName, 'NO MATCH', Comm FROM Salepeople
WHERE NOT City = ANY ( SELECT City FROM Customers )
ORDER BY 2 DESC;
Xar gal bir necha so‘rovlarni jamlaganda yumaloq qavslar yordamida baxolash mezonini
ko‘rsatishingiz mumkin. YA’ni
query X UNION query Y UNION query Z;
o‘rniga, yoki
( query X UNION query Y )UNION query Z;
yoki
query X UNION ( query Y UNION query Z );
ko‘rsatishingiz mumkin. CHunki UNION bitta dublikatlarni yo‘qotib boshqasini qoldirishi
mumkin.
Quyidagi ikki ifoda
( query X UNION ALL query Y )UNION query Z;
query X UNION ALL( query Y UNION query Z );
bir xil natija qaytarishi shart emas, agar ikkilangan satrlar unda o‘chirilgan bo‘lsa.
1
INSERT INTO Pul_birlik (Pul_birlik ) VALUES ('rubl');
Bitta maydonga qo‘shish
2
INSERT INTO Mijoz ( Ism, telefon, manzil ) Values ( 'Dilshod' ,
'987-98-98', 'Yunusobod');
Uchta maydonga qo‘shish
3
SELECT nomi, turi FROM dori;
Ikkita ustunni ajratish
4
SELECT TOP 5 id, Ism,Telefon,manzil FROM mijoz;
5 ta qator
5
DELETE FROM Dori
Ӯchirish
6
DELETE FROM dori WHERE turi= 'poroshok'
Poroshoklarni o‘chirish
7
UPDATE Mijoz SET ism = 'MASUDA'
WHERE ism='Masuda';
«Masuda» ni 'MASUDA' ga
yangilash
8
UPDATE Mijoz SET manzil = 'YUNUSOBOD';
Manzilni
xammasini
'YUNUSOBOD'
ga
o‘zgartirdi
9
UPDATE mijoz SET ism = 'DILSHOD', Telefon = '333-00-00'
WHERE ism = 'Dilshod';
'Dilshod' so‘zini yangiladi
10
SELECT nomi, soni*1.2, pul_birlik
FROM dori;
Matematik amal
11
SELECT id, nomi, turi, soni-10, pul_birlik
FROM dori
WHERE (soni>=50
And soni<=200);
Mantiqiy «va»
12
SELECT nomi, vaqti
Mantiqiy inkor
FROM Online_zakaz
WHERE Not (holat=0);
13
SELECT id, nomi, turi, soni-10, pul_birlik
FROM dori
WHERE (soni>=50 or soni<=200) and nomi='analgin';
And,or -
14
SELECT DISTINCT nomi
FROM online_zakaz;
Ustundagi bir xil qiymatlarni
birlashtiradi
15
SELECT DISTINCT pul_birlik
FROM dori;
16
SELECT nomi, soni, pul_birlik
FROM dori
WHERE soni between 10 and 30;
Oraliqni ko‘rsatish
17
SELECT nomi, soni, pul_birlik
FROM dori
WHERE soni Not In (10,50,200,800);
To‘plamga tegishli emas
18
SELECT Ism,
telefon
FROM
Mijoz
WHERE Ism Like 'S*';
S
xarfidan
boshlanadigan
ismlar
19
SELECT Ism, telefon
FROM Mijoz
WHERE Ism Like '*a';
a xarfi bilan tugagan
20
SELECT Ism, telefon
FROM Mijoz
WHERE Ism Like '*S*';
So‘zda S xarfi ixtiyoriy joyda
Nazorat savollari
1. So’rov obyektining vazifasi.
2. SQL tilining qanday buyruqlarini bilasiz?