Home > Database > Mysql Tutorial > How can we use MySQL DISTINCT clause with WHERE and LIMIT clause?

How can we use MySQL DISTINCT clause with WHERE and LIMIT clause?

PHPz
Release: 2023-08-24 09:17:02
forward
1510 people have browsed it

我们如何将 MySQL DISTINCT 子句与 WHERE 和 LIMIT 子句一起使用?

By using the WHERE clause with the DISTINCT clause in a MySQL query, we set a condition based on which MySQL returns the unique rows of the result set. By using the LIMIT clause and the DISTINCT clause in a MySQL query, we actually provide the server with a range on the maximum number of unique rows to return in the result set.

Example

We can use WHERE and LIMIT clauses with DISTINCT on the table named "testing" as shown below-

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)
Copy after login

The above is the detailed content of How can we use MySQL DISTINCT clause with WHERE and LIMIT clause?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template