MySQL 使用DISTINCT去除重复数据 2022/09/14

🔥distinct的使用🔥

distinct用于在查询中返回列的唯一不同值(去重复),支持单列或多列。在实际的应用中,表中的某一列含有重复值是很常见的,如employ表的dept列。如果在查询数据时,希望得到某列的所有不同值,可以使用distinct。

distinct语法

select distinct column_name,column_name from table_name;
mysql> select * from employee;
+----+--------+------+--------+-------+
| id | name   | sex  | salary | dept  |
+----+--------+------+--------+-------+
|  1 | 张三   | 男   |   5500 | 部门A |
|  2 | 李四   | 男   |   4500 | 部门B |
|  3 | 张小妹 | 女   |   4500 | 部门B |
+----+--------+------+--------+-------+
3 rows in set (0.00 sec)

mysql> insert into employee(name,sex,salary,dept) values('张三','1',6000,'部门D');
Query OK, 1 row affected (0.01 sec)

mysql> select * from employee;
+----+--------+------+--------+-------+
| id | name   | sex  | salary | dept  |
+----+--------+------+--------+-------+
|  1 | 张三   | 男   |   5500 | 部门A |
|  2 | 李四   | 男   |   4500 | 部门B |
|  3 | 张小妹 | 女   |   4500 | 部门B |
|  4 | 张三   | 1    |   6000 | 部门D |
+----+--------+------+--------+-------+
4 rows in set (0.00 sec)
mysql> select * from employee;
+----+--------+------+--------+-------+
| id | name   | sex  | salary | dept  |
+----+--------+------+--------+-------+
|  1 | 张三   | 男   |   5500 | 部门A |
|  2 | 李四   | 男   |   4500 | 部门B |
|  3 | 张小妹 | 女   |   4500 | 部门B |
|  4 | 张三   | 1    |   6000 | 部门D |
+----+--------+------+--------+-------+
4 rows in set (0.00 sec)

mysql> select distinct name from employee;
+--------+
| name   |
+--------+
| 张三   |
| 李四   |
| 张小妹 |
+--------+
3 rows in set (0.00 sec)
mysql> select * from employee;
+----+--------+------+--------+-------+
| id | name   | sex  | salary | dept  |
+----+--------+------+--------+-------+
|  1 | 张三   | 男   |   5500 | 部门A |
|  2 | 李四   | 男   |   4500 | 部门B |
|  3 | 张小妹 | 女   |   4500 | 部门B |
|  4 | 张三   | 1    |   6000 | 部门D |
+----+--------+------+--------+-------+
4 rows in set (0.00 sec)

mysql> select distinct sex from employee;
+------+
| sex  |
+------+
| 男   |
| 女   |
| 1    |
+------+
3 rows in set (0.00 sec)

mysql> select distinct dept from employee;
+-------+
| dept  |
+-------+
| 部门A |
| 部门B |
| 部门D |
+-------+
3 rows in set (0.00 sec)

mysql> select dept from employee group by dept;
+-------+
| dept  |
+-------+
| 部门A |
| 部门B |
| 部门D |
+-------+
3 rows in set (0.00 sec)
mysql> select distinct username from foot;
+----------+
| username |
+----------+
| 大哥     |
| 大哥1    |
| 大哥2    |
| 大哥4    |
| 大哥5    |
| 大哥6    |
| 大哥7    |
| 大哥8    |
| 大哥9    |
+----------+
9 rows in set (0.00 sec)

mysql> select * from foot;
+----+----------+-------+------------+
| id | username | city  | visit_date |
+----+----------+-------+------------+
|  1 | 大哥     | 上海  | 2022-01-01 |
|  2 | 大哥1    | 上海1 | 2022-01-02 |
|  3 | 大哥2    | 上海2 | 2022-01-03 |
| 35 | 大哥4    | 上海4 | 2022-01-05 |
| 36 | 大哥5    | 上海5 | 2022-01-06 |
| 37 | 大哥6    | 上海6 | 2022-01-07 |
| 38 | 大哥6    | 上海6 | 2022-01-07 |
| 39 | 大哥7    | 上海7 | 2022-01-08 |
| 40 | 大哥8    | 上海8 | 2022-01-09 |
| 41 | 大哥9    | 上海9 | 2022-01-10 |
+----+----------+-------+------------+
10 rows in set (0.00 sec)
mysql> select distinct username,city from foot;
+----------+-------+
| username | city  |
+----------+-------+
| 大哥     | 上海  |
| 大哥1    | 上海1 |
| 大哥2    | 上海2 |
| 大哥4    | 上海4 |
| 大哥5    | 上海5 |
| 大哥6    | 上海6 |
| 大哥7    | 上海7 |
| 大哥8    | 上海8 |
| 大哥9    | 上海9 |
+----------+-------+
9 rows in set (0.00 sec)

mysql> select distinct username from foot;
+----------+
| username |
+----------+
| 大哥     |
| 大哥1    |
| 大哥2    |
| 大哥4    |
| 大哥5    |
| 大哥6    |
| 大哥7    |
| 大哥8    |
| 大哥9    |
+----------+
9 rows in set (0.00 sec)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

️ 邪神

你自己看着办,你喜欢打赏我就赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值