If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
अभी तक records को सिर्फ single table से fetch कर रहे थे , लेकिन क्यों हो अगर आपको एक से ज्यादा tables से data fetch करना हो ?
Well , जैसा कि आपको पता है कि MySQL एक relational database management है , जिसका सीधा सा मतलब है कि किसी एक table का data एक से अधिक tables में exist कर सकता है।
जहाँ पर हमें एक से अधिक tables से data fetch करना होता है वहां पर हम JOIN
use करते हैं।
MySQL Join एक Important SQL Command है जो एक से अधिक tables को joint करता है ताकि इससे एक नया detailed table बनता है। यह table दोनों tables से आधारित होती है।
MySQL में तीन Types के JOIN होते हैं:
INNER JOIN
LEFT JOIN
RIGHT JOIN
इस topic में हम Inner Join / Join के बारे में बात करेंगे -
Suppose, customers
name की एक table है , जिसमे कुछ इस तरह से records available हैं -
+----+-----------+-----------+ | id | name | city | +----+-----------+------------+ | 1 | Raju | Delhi | | 2 | Mohit | Mumbai | | 3 | Sonia | Bengluru | +----+-----------+-----------+
और एक दूसरी table भी है orders
नाम की जिसमे customer के orders हैं , जिसमे कुछ इस तरह से records हैं-
+----+--------------+----------+ | id | customer_id | total | +----+--------------+----------+ | 1 | 1 | 1200 | | 2 | 2 | 900 | | 3 | 3 | 1000 | | 4 | 1 | 500 | +----+-------------+-----------+
table में एक customer_id
name का field है जिसमे customers
table की id column की value है , जिससे हमें ये पता चलता है कि किस customer का इस table में record है।
जब हम INNER JOIN
का उपयोग इन दोनों tables के साथ करते हैं, तो हमें उस customer के सारे records दिखेंगे जिसका id orders
table में है।
अब अगर हमें यह fetch करना हो कि किस customer ने कौन सा order किया है तो उसके लिए कुछ इस तरह से query होगी।
SELECT
customers.name, orders.total
FROM
customers
INNER JOIN
orders ON customers.id = orders.customer_id;
Output
+--------+--------+ | name | total | +--------+--------+ | Raju | 1200 | | Mohit | 900 | | Sonia | 1000 | | Raju | 500 | +--------+--------+
तो देखा आपने किस तरह से Inner Join का use 2 tables को join करने के लिए किया गया है , हालाँकि INNER JOIN
लिखने की वजाय JOIN
भी लिख सकते हैं , JOIN का मतलब by default INNER JOIN ही होता है।
जैसे अगर नीचे दी गयी query को run करोगे तो same output ही मिलेगा।
SELECT
customers.name, orders.total
FROM
customers
JOIN
orders ON customers.id = orders.customer_id;
किसी भी तरह की Join query use करते समय हमेशा columns को table name के साथ access करें इससे आपका code भी readable होगा और confusion भी नहीं होगी।
हालाँकि जरूरी नहीं कि Join करते time सिर्फ एक ही condition दें , need के according आप AND
या OR
condition का use भी कर सकते हैं।
For Example
SELECT
column_list
FROM
table1
JOIN
table2 ON condition1 AND condition2 AND ..;
Or
SELECT
column_list
FROM
table1
JOIN
table2 ON condition1 OR condition2;
Need के according आप Aggregate Functions का use भी Join के साथ कर सकते हैं , जैसे कि ऊपर दी गयी 2 tables customers
और orders
से अगर मुझे single customer के total order search करना चाहूँ तो कुछ इस तरह से query होगी।
SELECT
customers.name, SUM(orders.total) AS total
FROM
customers
INNER JOIN
orders ON customers.id = orders.customer_id
GROUP BY
customers.id;
Output
+--------+--------+ | name | total | +--------+--------+ | Raju | 1700 | | Mohit | 900 | | Sonia | 1000 | +--------+--------+
ऊपर दिए गए example में customer.id
को Group By
का use किया गया क्योंकि हमें customers पर aggregate function SUM()
use करना था , हालाँकि इसके बारे में आप आगे detail में पढ़ेंगे।
i Hope, आपको MySQL में Inner Join Statement के बारे में अच्छे से समझ आ गया होगा।
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook