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 में आपने Select Statement के बारे में पढ़ा और समझा , इस topic में हम records को order के according fetch करना सीखेंगे।
MySQL में records select करते time आप किसी particular column के bases पर records को order से भी fetch कर सकते हैं , इसके लिए ORDER BY
Statement का use किया जाता है।
SELECT
column_list
FROM
table_name
ORDER BY
column1 [ASC|DESC],
column2 [ASC|DESC],
...;
MySQL में आप सिर्फ दो keywords का ही use किया जाता है records को sort करने के लिए -
ASC
: मतलब ascending order , इससे सभी records ascending order में fetch होंगे।
DESC
: और इससे सभी records descending order में fetch होंगे।
सबसे पहले हम table में देख लेते हैं कि कितने और किस type से records fetch हो रहे हैं , इसके लिए हम normally select *
statement का use करेंगे।
SELECT * FROM users;
Output
+----+--------+------------------+------------+---------+ | id | name | email | mobileno | address | +----+--------+------------------+------------+---------+ | 1 | Raju | askdj@gmail.com | 3243907889 | India | | 2 | Aman | lfd@gmail.com | 9043907889 | NULL | | 3 | Babloo | bsdnas@gmail.com | NULL | US | | 4 | john | Xaas@gmail.com | 9100021321 | UAE | | 5 | Shaam | Zaa@gmail.com | 9100021334 | Russia | | 6 | kaam | a@gmail.com | 9100021326 | Turkey | +----+--------+------------------+------------+---------+ 6 rows in set (0.001 sec)
Output में आप देख सकते हैं कि अभी records को जैसे insert किया गया था बैसे ही आ रहे हैं , अब हम name के according records को Ascending Order में fetch करेंगे।
SELECT * FROM users ORDER BY name ASC;
Output
+----+--------+------------------+------------+---------+ | id | name | email | mobileno | address | +----+--------+------------------+------------+---------+ | 2 | Aman | lfd@gmail.com | 9043907889 | NULL | | 3 | Babloo | bsdnas@gmail.com | NULL | US | | 4 | john | Xaas@gmail.com | 9100021321 | UAE | | 6 | kaam | a@gmail.com | 9100021326 | Turkey | | 1 | Raju | askdj@gmail.com | 3243907889 | India | | 5 | Shaam | Zaa@gmail.com | 9100021334 | Russia | +----+--------+------------------+------------+---------+ 6 rows in set (0.001 sec)
Output में आप देख सकते हैं कि सभी records name के according sort हुए हैं। और इसी तरह से आप records को Descending order में fetch कर सकते हैं इसके लिए simply ASC
की जगह DESC
use करना है।
जैसा कि आपने ऊपर दिए गए syntax में देखा , आप एक साथ कई columns को भी sort करके records fetch कर सकते हैं।
जैसे नीचे example में name और address columns को sort किया गया है।
SELECT * FROM users ORDER BY name ASC, email DESC;
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