Python MySQL Join
In this page:
cursor.execute(
"SELECT a.column, b.column FROM table_a AS a "
"INNER JOIN table_b AS b ON a.key = b.key"
)
Basic INNER JOIN
cursor.execute("SELECT users.name, products.name FROM users INNER JOIN products ON users.fav = products.id") users और products tables की matching rows को जोड़ता है, जहाँ भी किसी user का favorite (fav) column किसी product की id से मेल खाता है -- जिन users का कोई matching product नहीं है वे result से पूरी तरह बाहर रह जाते हैं।
उदाहरण: Basic INNER JOIN
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
"SELECT users.name, products.name FROM users "
"INNER JOIN products ON users.fav = products.id" # only rows with a match in both tables are included
)
print("Only users with a matching favorite product are included")
LEFT JOIN: Unmatched Rows को शामिल करना
LEFT JOIN, left (पहले नामित) table की हर row return करता है चाहे right table में उसका कोई match हो या न हो -- जब कोई match नहीं होता, तो right table के columns बस NULL आ जाते हैं, जो उदाहरण के लिए हर customer, यहाँ तक कि जिन्होंने कभी order नहीं दिया, ढूँढने के लिए उपयोगी है।
उदाहरण: LEFT JOIN: Including Unmatched Rows
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
"SELECT customers.name, orders.id FROM customers "
"LEFT JOIN orders ON customers.id = orders.customer_id" # every customer included, even with no matching order
)
print("Customers with no orders still appear, with NULL order id")
तीन या ज़्यादा Tables Join करना
एक ही query में कई JOIN clauses को chain किया जा सकता है ताकि तीन या ज़्यादा related tables का data जोड़ा जा सके -- उदाहरण के लिए orders को customers से link करना, और अलग से हर order को उसमें मौजूद products से link करना, सब एक ही statement में।
उदाहरण: Joining Three or More Tables
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
"SELECT c.name, o.id, p.name FROM customers c "
"JOIN orders o ON c.id = o.customer_id " # first join links customers to orders
"JOIN products p ON o.product_id = p.id" # second join links orders to products
)
print("Three tables combined with short aliases")
Ambiguous Column Errors से बचना
जब दो joined tables का column name एक जैसा हो (जैसे दोनों में एक "id" column), तो SELECT list या WHERE clause में सिर्फ उस नाम को reference करना ambiguous होता है और एक error raise करता है -- इसे table name (या alias) से qualify करना, जैसे customers.id, यह ambiguity हल कर देता है।
उदाहरण: Avoiding Ambiguous Column Errors
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
"SELECT customers.id, orders.id FROM customers "
"JOIN orders ON customers.id = orders.customer_id" # table names qualify the ambiguous "id" column
)
print("Qualifying 'id' with a table name avoids ambiguity")
WHERE, ORDER BY और LIMIT के साथ Joins
एक JOIN, पहले से बताई गई हर SELECT clause के साथ naturally combine होता है -- joined result को filter करने के लिए WHERE, उसे sort करने के लिए ORDER BY, और कितनी combined rows वापस आएँ यह सीमित करने के लिए LIMIT -- और यह उसी logical order में लागू होता है: पहले join, फिर filter, फिर sort, फिर limit।
उदाहरण: Joins with WHERE, ORDER BY, and LIMIT
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
"SELECT c.name, o.total FROM customers c JOIN orders o ON c.id = o.customer_id "
"WHERE o.total > %s ORDER BY o.total DESC LIMIT 5", # join, then filter, then sort, then limit
(100,),
)
print("Join, then filter, then sort, then limit")
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Python MySQL Get Started
- Python MySQL Create Database
- Python MySQL Create Table
- Python MySQL Insert Into Table
- Python MySQL Select From Table
- Python MySQL Select With a Filter
- Python MySQL Order By
- Python MySQL Delete Record
- Python MySQL Drop Table
- Python MySQL Update Table
- Python MySQL Limit
- Python MySQL Join