MySQL OR Operator In Hindi

📔 : MySQL 🔗

MySQL में OR operator एक logical operator है जो दो या दो से अधिक boolean expression को combine करके 1 , 0 या NULL return करता है। normally OR operator तब 1 (true) return करता है जब compare किये जाने वाले expressions में से कोई भी एक true हो otherwise false .

For Example

true AND true = 1; true AND false = 1; false AND false = 0;

आप चाहे तो directly query run करके देख लेते हैं , किन combinations के साथ हमें 1 (true) या 0 (false) मिलेगा।

SELECT 1 OR 0, 0 OR 1, 0 OR 0, 1 OR 1;
+--------+--------+--------+--------+
| 1 OR 0 | 0 OR 1 | 0 OR 0 | 1 OR 1 |
+--------+--------+--------+--------+
|      1 |      1 |      0 |      1 |
+--------+--------+--------+--------+
1 row in set (0.041 sec)

ऊपर दिया गया example तो 1 और 0 के साथ का है अब NULL के साथ का देख लेते हैं -

SELECT 1 OR NULL, NULL OR NULL, 0 OR NULL;

Output

+-----------+--------------+-----------+
| 1 OR NULL | NULL OR NULL | 0 OR NULL |
+-----------+--------------+-----------+
|         1 |         NULL |      NULL |
+-----------+--------------+-----------+
1 row in set (0.000 sec)

Use of OR Operator

MySQL में OR operator का use Where Clause के साथ , Table Joining में या किसी particular if else condition के साथ किया सकता है।

MySQL OR Operator Example

Example के लिए हमारे पास एक users table है , सबसे पहले normally सभी records को fetch करके देखते हैं -

SELECT * FROM users;

Output

+----+--------+------------------+------------+---------+--------+
| id | name   | email            | mobileno   | address | salary|
+----+--------+------------------+------------+---------+--------+
|  1 | Raju   | askdj@gmail.com  | 3243907889 | India   |   1000 |
|  2 | Aman   | lfd@gmail.com    | 9043907889 | NULL    |   1200 |
|  3 | Babloo | bsdnas@gmail.com | NULL       | US      |   1030 |
|  4 | john   | Xaas@gmail.com   | 9100021321 | UAE     |    100 |
|  5 | Shaam  | Zaa@gmail.com    | 9100021334 | Russia  |    200 |
|  6 | kaam   | a@gmail.com      | 9100021326 | Turkey  |   1500 |
+----+--------+------------------+------------+---------+--------+
6 rows in set (0.000 sec)

Same table से अब हम वो records fetch करेंगे जहाँ पर salary 200 है या 100 है -

SELECT * FROM users WHERE salary = 100 OR salary = 100;

Output

+----+-------+----------------+------------+---------+--------+
| id | name  | email          | mobileno   | address | salary |
+----+-------+----------------+------------+---------+--------+
|  4 | john  | Xaas@gmail.com | 9100021321 | UAE     |    100 |
|  5 | Shaam | Zaa@gmail.com  | 9100021334 | Russia  |    200 |
+----+-------+----------------+------------+---------+--------+
2 rows in set (0.001 sec)

Output में आप देख सकते हैं कि हमें ठीक condition के according ही records मिले हैं। हालाँकि आप OR की जगह || का use भी कर सकते हैं।

For Example

SELECT * FROM users WHERE salary = 100 || salary = 200;

यह जरूरी नहीं है कि 2 expressions को use करें , need के according आप कितनीं ही conditions का use कर सकते हैं , जैसे

SELECT * FROM users WHERE salary = 100 OR salary = 200 OR address = 'India';

Output

+----+-------+-----------------+------------+---------+--------+
| id | name  | email           | mobileno   | address | salary |
+----+-------+-----------------+------------+---------+--------+
|  1 | Raju  | askdj@gmail.com | 3243907889 | India   |   1000 |
|  4 | john  | Xaas@gmail.com  | 9100021321 | UAE     |    100 |
|  5 | Shaam | Zaa@gmail.com   | 9100021334 | Russia  |    200 |
+----+-------+-----------------+------------+---------+--------+
3 rows in set (0.001 sec)

MySQL Operator precedence

जब किसी condition में AND और OR दोनों operators का use होता है , तो MySQL उस expression को evaluate / calculate करने के लिए operator precedence का use करती है। और higher precedence वाले expression पहले calculate होते हैं।

अब AND और OR operator में AND operator की higher precedence है इसलिए यह पहले evaluate होगा , इस आप query run करके भी हैं।

SELECT 1 OR 0 AND 0;

Output

+--------------+
| 1 OR 0 AND 0 |
+--------------+
|            1 |
+--------------+
1 row in set (0.000 sec)

Example में दिए गए expression में 1 OR 0 AND 0 evaluate करते time सबसे पहले 0 AND 0 evaluate होगा जो 0 (false) return करेगा उसके बाद 1 OR 0 बचेगा जो finally 1 (true) return करेगा।

इसे आप नीचे दिए गए statements से समझ सकते हैं।

1 OR 0 AND 0; 1 OR (0 AND 0); // 0 AND 0 returns 0. 1 OR 0; // result 1;

तो कुछ इस तरह से MySQL में OR operator work करता है , I Hope आपको समझ आया होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

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

b2eprogrammers