PostgreSQL LEFT JOIN
CHAPGA QO‘SHILING
LEFT JOIN kalit so‘zi “chap” jadvaldan BARCHA yozuvlarni, “o‘ng” jadvaldan mos yozuvlarni tanlaydi. Natija, agar mos kelmasa, o‘ng tomondan 0 ta yozuv.
Keling, soxta sinov mahsulotlari jadvalimizdan foydalangan holda misolni ko‘rib chiqaylik:
testproduct_id | product_name
| category_id
----------------+------------------------+-------------
1 | Johns Fruit Cake |
3
2 | Marys Healthy Mix |
9
3 | Peters Scary Stuff |
10
4 | Jims Secret Recipe |
11
5 | Elisabeths Best Apples |
12
6 | Janes Favorite Cheese |
4
7 | Billys Home Made Pizza |
13
8 | Ellas Special Salmon |
8
9 | Roberts Rich Spaghetti |
5
10 |
Mias Popular Ice |
14
(10 rows)
Biz toifalar jadvali bilan sinov mahsulotlari jadvaliga qo‘shilishga harakat qilamiz:
category_id | category_name |
description
-------------+----------------+------------------------------------------------------------
1 | Beverages | Soft drinks, coffees, teas,
beers, and ales
2 | Condiments | Sweet and savory sauces, relishes,
spreads, and seasonings
3 | Confections | Desserts, candies, and sweet breads
4 | Dairy Products | Cheeses
5 | Grains/Cereals | Breads, crackers, pasta, and cereal
6 | Meat/Poultry | Prepared meats
7 | Produce | Dried fruit and bean
curd
8 |
Seafood | Seaweed and fish
(8
rows)
Eslatma: Sinov mahsulotlaridagi ko‘pgina mahsulotlarda kategoriyalar jadvalidagi toifalarning hech biriga mos kelmaydigan kategoriya_id mavjud.
LEFT JOIN-dan foydalanib, biz test mahsulotlaridagi barcha yozuvlarni, hatto toifalar jadvalida mos kelmaganlarni ham olamiz:
Misol
Kategoriya_id ustunidan foydalanib, test mahsulotlarini toifalarga qo‘shing:
SELECT
testproduct_id, product_name, category_name
FROM testproducts
LEFT JOIN categories ON testproducts.category_id = categories.category_id;
Misolni ishga tushirish »
Natija
Sinov mahsulotlaridagi barcha yozuvlar va faqat toifalardagi mos yozuvlar:
testproduct_id | product_name
| category_name
----------------+------------------------+----------------
1 | Johns Fruit Cake | Confections
2 | Marys Healthy Mix |
3 | Peters Scary Stuff |
4 | Jims Secret Recipe |
5 | Elisabeths Best Apples |
6 | Janes Favorite Cheese | Dairy Products
7 | Billys Home Made Pizza |
8 | Ellas Special Salmon | Seafood
9 | Roberts Rich Spaghetti | Grains/Cereals
10 | Mias Popular Ice |
(10 rows)
Eslatma: LEFT JOIN va LEFT OUTER JOIN bir xil natija beradi.
OUTER - LEFT JOIN uchun standart birlashma turi, shuning uchun siz LEFT JOIN yozganingizda tahlilchi LEFT OUTER JOIN yozadi.
W3Schools Pathfinder
Yutuqlaringizni kuzating – bu bepul!
