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.
पिछले topic में आपने Left Join के बारे में पढ़ा और समझा , इस topic में हम Right Join के बारे में बात करेंगे।
Right Join
, Left Join के जैसी ही है बस tables को reverse कर दीजिये , मतलब Left Join में Left Table (table1) से सभी records आते थे और जिस table को left join (table2) करते थे उसमे से सिर्फ matched records .
जबकि Right Join में Right Table (table2) से सभी records आते हैं और left table (table1) से सिर्फ matched records .
SELECT
column_list
FROM table1
RIGHT JOIN table2
ON join_condition;
Explanation
Syntax में table1
left table है और table2
right table है , इसीलिए जब सभी records left table से आ रहे थे तो उसे Left Join कहते है ,और जब सभी records Right table से आये तो उसे Right Join कहते हैं।
अब इसे समझने के लिए कुछ live examples देख लेते हैं ।
Suppose, हमारे पास users
name की एक table है , जिसमे कुछ इस तरह से records available हैं।
+----+-------------+ | id | name | +----+-------------+ | 1 | Tom | | 2 | Tom Holland | | 4 | john Doe | | 5 | John Quill | +----+-------------+
और एक दूसरी table भी है user_orders
नाम की जिसमे user के orders हैं कि user ने कौन सा product किस price पर buy किया है , और कुछ इस तरह से records मौजूद हैं।
+----+---------+---------+-------+ | id | user_id | product | total | +----+---------+---------+-------+ | 1 | 1 | Shoes | 455 | | 2 | 2 | Clothes | 200 | | 3 | 2 | Mobile | 20000 | | 4 | 1 | Grocery | 999 | | 5 | 1 | Books | 1100 | +----+---------+---------+-------+
मुझे सिर्फ ये पता करना है कि किस user ने कौन सा order किया है , उसके लिए कुछ इस तरह से query होगी।
SELECT
user_orders.id AS order_id, users.name, user_orders.product, user_orders.total
FROM users
RIGHT JOIN
user_orders ON user_orders.user_id=users.id
GROUP BY user_orders.id;
Output
+----------+-------------+---------+-------+ | order_id | name | product | total | +----------+-------------+---------+-------+ | 1 | Tom | Shoes | 455 | | 2 | Tom Holland | Clothes | 200 | | 3 | Tom Holland | Mobile | 20000 | | 4 | Tom | Grocery | 999 | | 5 | Tom | Books | 1100 | +----------+-------------+---------+-------+
Output में आप देख सकते हैं कि Right Table (user_orders
) से सभी records आये हैं , लेकिन Left Table (users
) से सिर्फ matched records ही आये हैं।
Example में मैंने AS Operator का use भी किया है ताकि column name में कोई confusion न हो , आप need के according कुछ भी name दे सकते हैं ।
जैसे column को AS Operator का use करके alias नाम दिया बैसे ही आप चाहे तो table name को alias नाम दे सकते हैं। जैसे नीचे दी गयी query भी same result generate करेगी।
SELECT
uo.id AS order_id, u.name, uo.product, uo.total
FROM users AS u
RIGHT JOIN
user_orders AS uo ON uo.user_id=u.id
GROUP BY uo.id;
अब कुछ और Right Join के use cases देख लेते हैं।
1. अगर पता करना हो कि किसी particular user ने कुल कितने products buy करके कितने रुपये का total pay किया है , इसके लिए कुछ इस तरह से query होगी -
SELECT
users.id AS user_id, users.name, COUNT(user_orders.product) AS total_products, GROUP_CONCAT(user_orders.product) AS products, SUM(user_orders.total) AS paid_amount
FROM users
Right JOIN
user_orders ON user_orders.user_id=users.id
GROUP BY users.id;
Output
+---------+-------------+----------------+---------------------+-------------+ | user_id | name | total_products | products | paid_amount | +---------+-------------+----------------+---------------------+-------------+ | 1 | Tom | 3 | Shoes,Grocery,Books | 2554 | | 2 | Tom Holland | 2 | Clothes,Mobile | 20200 | +---------+-------------+----------------+---------------------+-------------+
तो कुछ तरह से हम MySQL में Right Join का use करते हैं , I know ऊपर दिए गए example में कुछ advance concept जैसे If , Sum या Group By use किये गए लेकिन tension not :) आगे आप इनके बारे में पढ़ेंगे।
I Hope, आपको Right Join
के बारे में अच्छे से समझ आ गया होगा।
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook