通过在MySQL查询中将WHERE子句与DISTINCT子句一起使用,我们将基于一个条件,即MySQL返回结果集的唯一行。通过在MySQL查询中使用LIMIT子句和DISTINCT子句,我们实际上为服务器提供了有关返回的结果集的最大唯一行数的范围。
示例
我们可以在名为“ testing”的表上按如下所示将WHERE和LIMIT子句与DISTINCT一起使用-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)