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 में आपने DELETE Statement के बारे में पढ़ा और समझा कि कैसे किसी table से records को delete किया जाता है , लेकिन क्या हो अगर आपको किसी record से related सभी reference records को भी delete करना हो ?
Actually MySQL में JOIN Statements को records को select करने के अलावा records को delete करने के लिए भी किया जाता है , delete के साथ join का use करके आप उस record के parent / child records को भी एक साथ delete कर सकते हैं।
DELETE table1, table2 FROM table1
INNER JOIN table2 ON table1.column=table2.column
WHERE [condition if any];
Explanation
यहां , table1
और table2 वो दो tables
हैं जिनके records आपको एक साथ delete करने के हैं , फिर table2 को join
किया गया condition का bases पर और finally अगर कोई Where Condition है तो उसका use किया गया है।
ध्यान रहे कि आप DELETE और FROM के बीच से table1
को हटा देते हैं, तो DELETE query केवल table2 से ही records को remove करेगी , इसी तरह यदि आप table2
को हटा देते हैं, तो DELETE query केवल table1 से rows को remove करेगा देगा।
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 | +----+---------+---------+-------+
example के लिए मैं users table से id=1
के record के साथ उसके orders भी delete करना चाहूंगा , तो उसके लिए कुछ इस तरह से query होगी -
DELETE users, user_orders FROM users
INNER JOIN user_orders ON user_orders.user_id=users.id
WHERE users.id=1;
Output
Query OK, 4 rows affected (0.057 sec)
Output में 4 rows affected आया है इसका मतलब है कि दोनों table से 4 records delete हुए हैं। क्योंकि user_orders
में से 3 records और users
table से 1 record delete हुआ है।
example में तो सिर्फ 2 tables से data delete किया गया है आप need के according इससे ज्यादा tables भी join कराकर multiple tables से data delete कर सकते हैं।
इसी तरह से आप Left Join का use करके भी data delete कर सकते हैं , हालाँकि Left Join Statement के working के according main table से records delete होंगे ही लेकिन reference table से तभी records delete होंगे जब join condition match होगी।
जैसे user_orders
table में user id
4 , 5 के orders नहीं है , तो अगर मैं left join के साथ delete query run करूंगा तो सिर्फ users table से ही data delete होगा।
For Example
DELETE users, user_orders FROM users
LEFT JOIN user_orders ON user_orders.user_id=users.id
WHERE users.id=4;
Query OK, 1 row affected (0.042 sec)
Output में आप देख सकते हैं कि सिर्फ एक ही record delete हुआ है , जो कि users table से है।
बाकी delete join के साथ आप अपनी जरूरत के हिसाब से और भी experiment कर सकते हैं।
I Hope, आपको MySQL में Delete With 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