MySQL View In Hindi | View In MySQL In Hindi

📔 : MySQL 🔗

MySQL में View को समझने से पहले एक example समझ लेते हैं।

Suppose, हमारे पास users name की एक table है , जिसमे कुछ इस तरह से records available हैं।

+----+------------+---------------+
| id | name       | address       |
+----+------------+---------------+
|  2 | Tom Cruise | India         |
|  5 | John Quill | India         |
|  7 | Wick       | Address       |
|  8 | Rohan      | ROhan Address |
+----+------------+---------------+

और एक दूसरी table भी है user_orders नाम की जिसमे user के orders हैं कि user ने कौन सा product किस price पर buy किया है , और कुछ इस तरह से records मौजूद हैं।

+----+---------+---------+-------+---------------------+
| id | user_id | product | total | created_at          |
+----+---------+---------+-------+---------------------+
|  2 |       2 | Clothes |   500 | 2023-02-26 03:49:43 |
|  3 |       2 | Mobile  | 20000 | 2023-02-26 08:32:25 |
|  6 |       7 | Charger |  1800 | 2023-03-20 06:30:16 |
+----+---------+---------+-------+---------------------+

नीचे दी गयी query दोनों tables से data fetch करके दे रही है -

SELECT users.id, users.name, GROUP_CONCAT(user_orders.product) AS buy_products , SUM(user_orders.total) AS total_order FROM users LEFT JOIN user_orders ON user_orders.user_id=users.id GROUP BY users.id;

Output

+----+------------+----------------+-------------+
| id | name       | buy_products   | total_order |
+----+------------+----------------+-------------+
|  2 | Tom Cruise | Mobile,Clothes |       20500 |
|  5 | John Quill | NULL           |        NULL |
|  7 | Wick       | Charger        |        1800 |
|  8 | Rohan      | NULL           |        NULL |
+----+------------+----------------+-------------+

अब अगर मुझे इस तरह से जब data चाहिए होगा मुझे same query बार बार run करनी होगी।

अब अगर आप बार बार query लिखने से बचना चाहते हैं तो एक तो तरीका है कि आप किसी file में query save करे लें और use में ले , दूसरा तरीका है एक View create करें और use करें।

MySQL Create View

view create करने के लिए CREATE VIEW command का use किया जाता है , जैसे कि ऊपर दिए गए example के लिए कुछ इस तरह से view create करेंगे -

CREATE VIEW userOrders AS SELECT users.id, users.name, GROUP_CONCAT(user_orders.product) AS buy_products , SUM(user_orders.total) AS total_order FROM users LEFT JOIN user_orders ON user_orders.user_id=users.id GROUP BY users.id;

Output

Query OK, 0 rows affected (0.078 sec)

Query OK, होने का मतलब है view create हो गया है।

अब आपको बार बार same query लिखने की जरूरत नहीं , आप data को view से ही select कर सकते हैं , जैसे -

SELECT * FROM userOrders;
+----+------------+----------------+-------------+
| id | name       | buy_products   | total_order |
+----+------------+----------------+-------------+
|  2 | Tom Cruise | Mobile,Clothes |       20500 |
|  5 | John Quill | NULL           |        NULL |
|  7 | Wick       | Charger        |        1800 |
|  8 | Rohan      | NULL           |        NULL |
+----+------------+----------------+-------------+

चूंकि view को एक table की तरह use किया जा रहा है तो आप particular columns को भी select कर सकते हैं या WHERE Clause , LIMIT भी apply कर सकते हैं।

SELECT name, total_order FROM userOrders WHERE total_order IS NOT NULL;
+----+------------+----------------+-------------+
| id | name       | buy_products   | total_order |
+----+------------+----------------+-------------+
|  2 | Tom Cruise | Mobile,Clothes |       20500 |
|  7 | Wick       | Charger        |        1800 |
+----+------------+----------------+-------------+

इसके अलावा हम नीचे दिए गए View topics के बारे में बात करेंगे।

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