Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> MySQL

MySQL में टॉप N पंक्तियों को कुशलतापूर्वक चुनने की क्वेरी

टॉप N पंक्तियों को कुशलतापूर्वक चुनने के लिए इंडेक्स का उपयोग करें।

उदाहरण

टेबल बनाएं:

mysql> create table DemoTable (StudentName varchar(100), StudentScore int);
Query OK, 0 rows affected (0.66 sec)

रिकॉर्ड डालें:

mysql> insert into DemoTable values('John', 34);
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values('Carol', 55);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('Bob', 58);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('Sam', 38);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('Mike', 48);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('Adam', 41);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('Chris', 47);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values('Robert', 40);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values('David', 89);
Query OK, 1 row affected (0.18 sec)

सभी रिकॉर्ड देखें:

mysql> select * from DemoTable;

आउटपुट

+-------------+--------------+
| StudentName | StudentScore |
+-------------+--------------+
| John        | 34           |
| Carol       | 55           |
| Bob         | 58           |
| Sam         | 38           |
| Mike        | 48           |
| Adam        | 41           |
| Chris       | 47           |
| Robert      | 40           |
| David       | 89           |
+-------------+--------------+
9 rows in set (0.00 sec)

इंडेक्स बनाकर टॉप N पंक्तियां कुशलतापूर्वक चुनें:

mysql> alter table DemoTable ADD INDEX name_score(StudentName, StudentScore);
Query OK, 0 rows affected (0.61 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select StudentName, StudentScore from DemoTable order by StudentScore LIMIT 5, 3;

आउटपुट

+-------------+--------------+
| StudentName | StudentScore |
+-------------+--------------+
| Mike        | 48           |
| Carol       | 55           |
| Bob         | 58           |
+-------------+--------------+
3 rows in set (0.00 sec)

व्याख्या: ORDER BY StudentScore आरोही क्रम में सॉर्ट करता है। LIMIT 5, 3 का अर्थ है 5 पंक्तियां छोड़ें, फिर 3 पंक्तियां लें (6th, 7th, 8th उच्चतम स्कोर)।

  1. - MySQL में रिकर्सिव SELECT क्वेरी कैसे करें

    रिकर्सिव सेलेक्ट के लिए, आइए एक उदाहरण देखते हैं। सबसे पहले, हम एक टेबल बनाएंगे। टेबल बनाने के लिए CREATE कमांड का उपयोग किया जाता है। ); Query OK, 0 rows affected (0.61 sec) अब, हम टेबल tblSelectDemo में रिकॉर्ड्स डालेंगे। insert into tblSelectDemo values(5,Bob); Query OK, 1 row affected (0.18

  1. - MySQL SELECT क्वेरी: किसी टेबल से उन रो को चुनें जो दूसरी टेबल में नहीं हैं

    हमारे उदाहरण के लिए, हम दो टेबल बनाएंगे और नेचुरल लेफ्ट जॉइन लागू करके उन रो को प्राप्त करेंगे जो पहली टेबल में हैं लेकिन दूसरी में नहीं। पहली टेबल बनाना। mysql> create table FirstTableDemo -> ( -> id int, -> name varchar(100) -> ); Query OK, 0 rows affected (0.48 sec) पहली ट

  1. MySQL से अंतिम 10 रो कैसे चुनें? MySQL से अंतिम 10 रो कैसे चुनें?

    MySQL से अंतिम 10 रो चुनने के लिए, हम SELECT स्टेटमेंट और LIMIT कॉन्सेप्ट के साथ सबक्वेरी का उपयोग कर सकते हैं। निम्न एक उदाहरण है। टेबल बनाना। mysql> create table Last10RecordsDemo -> ( -> id int, -> name varchar(100) -> ); Query OK, 0 rows affected (0.75 sec) टेबल में रिकॉर