Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

การใช้ DISTINCT ร่วมกับ WHERE และ LIMIT ใน MySQL


WHERE กรองแถวก่อน DISTINCT คัดเฉพาะแถวที่ไม่ซ้ำ LIMIT จำกัดจำนวนแถวคืน ตัวอย่างตาราง testing:

mysql> Select * from testing;
+------+---------+---------+
| id   | fname   | Lname   |
+------+---------+---------+
|  200 | Raman   | Kumar   |
|  201 | Sahil   | Bhalla  |
|  202 | Gaurav  | NULL    |
|  203 | Aarav   | NULL    |
|  204 | Harshit | Khurana |
|  205 | Rahul   | NULL    |
|  206 | Piyush  | Kohli   |
|  207 | Lovkesh | NULL    |
|  208 | Gaurav  | Kumar   |
|  209 | Raman   | Kumar   |
+------+---------+---------+
10 rows in set (0.00 sec)
mysql> Select DISTINCT Lname from testing where Lname IS NOT NULL limit 3;
+---------+
| Lname   |
+---------+
| Kumar   |
| Bhalla  |
| Khurana |
+---------+
3 rows in set (0.00 sec)

คิวรี่กรอง Lname IS NOT NULL แล้วคัด distinct แล้วจำกัด 3 แถว