Self JOIN

Self join - bu odatiy join, lekin jadval o'z-o'ziga qo'shiladi.

SU

SQL Self Join

Self join - bu odatiy join, lekin jadval o'z-o'ziga qo'shiladi.

Self Join Sintaksisi

SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;

T1 va T2 - bu bir xil jadval uchun turli jadval aliaslari.

Demo Ma'lumotlar Bazasi

Ushbu o'quv qo'llanmada biz mashhur Northwind namunaviy ma'lumotlar bazasidan foydalanamiz.

Quyida "Customers" jadvalidan bir tanlov:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico

SQL Self Join Misoli

Quyidagi SQL bayonoti bir xil shaharidan kelgan mijozlarni moslashtiradi:

SELECT A.CustomerName AS CustomerName1, B.CustomerName AS CustomerName2, A.City
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
AND A.City = B.City
ORDER BY A.City;

Ushbu sahifada

Xato haqida xabar berish