Home > Database > Mysql Tutorial > body text

How to filter out duplicate rows in the rows returned by MySQL?

PHPz
Release: 2023-08-25 08:01:05
forward
1251 people have browsed it

How to filter out duplicate rows in the rows returned by MySQL?

This can be achieved by using the DISTINCT keyword in the SELECT clause. DISTINCT applies to all combinations of data fields specified in the SELECT clause.

Example

We have the applied table "Student" DISTINCT keyword as follows -

mysql> Select * from student;

+------+---------+---------+-----------+
| Id   | Name    | Address | Subject   |
+------+---------+---------+-----------+
| 1    | Gaurav  | Delhi   | Computers |
| 2    | Aarav   | Mumbai  | History   |
| 15   | Harshit | Delhi   | Commerce  |
| 17   | Raman   | Shimla  | Computers |
| 20   | Gaurav  | Jaipur  | History   |
+------+---------+---------+-----------+

5 rows in set (0.00 sec)

mysql> Select DISTINCT Address from student;

+---------+
| Address |
+---------+
| Delhi   |
| Mumbai  |
| Shimla  |
| Jaipur  |
+---------+

4 rows in set (0.00 sec)

mysql> Select DISTINCT * from student;

+------+---------+---------+-----------+
| Id   | Name    | Address | Subject   |
+------+---------+---------+-----------+
| 1    | Gaurav  | Delhi   | Computers |
| 2    | Aarav   | Mumbai  | History   |
| 15   | Harshit | Delhi   | Commerce  |
| 17   | Raman   | Shimla  | Computers |
| 20   | Gaurav  | Jaipur  | History   |
+------+---------+---------+-----------+

5 rows in set (0.00 sec)

mysql> Select DISTINCT name from student;

+---------+
| name    |
+---------+
| Gaurav  |
| Aarav   |
| Harshit |
| Raman   |
+---------+

4 rows in set (0.00 sec)
Copy after login

The above is the detailed content of How to filter out duplicate rows in the rows returned by MySQL?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!