首页 >数据库 >mysql教程 >MySQL返回结果集的行中如何过滤掉重复的行?

MySQL返回结果集的行中如何过滤掉重复的行?

PHPz
PHPz转载
2023-08-25 08:01:051290浏览

MySQL返回结果集的行中如何过滤掉重复的行?

可以通过在 SELECT 子句中使用 DISTINCT 关键字来实现。 DISTINCT 适用于 SELECT 子句中指定的所有数据字段的组合。

示例

我们有已应用的表“Student” DISTINCT 关键字如下 -

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)

以上是MySQL返回结果集的行中如何过滤掉重复的行?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除