mysql学习笔记,用distinct去除重复数据

大家好,我是天空之城,今天给大家带来,用distinct去除重复数据,支持单列或多列

语法
SELECT DISTINCT column name, column name(会作用于两个字段,组合去重)
FROM table_ name;

还是用上次employee表格练习
mysql> select distinct sex from employee;
+------+
| sex  |
+------+
| 男   |
| 女   |
+------+

mysql> select distinct dept from employee;
+-------+
| dept  |
+-------+
| 部门A |
| 部门C |
| 部门B |
+-------+

这里建立一个新表来演示
create table footprint(
	id int not null auto_increment primary key,
	username varchar(30) comment '用户名',
	city varchar(30) comment '城市',
	visit_date varchar(10) comment '到访日期'
);

insert into footprint(username, city, visit_date) values('liusa', '贵阳', '2019-12-05');
insert into footprint(username, city, visit_date) values('liusa', '贵阳', '2020-01-15');
insert into footprint(username, city, visit_date) values('liusa', '北京', '2018-10-10');
insert into footprint(username, city, visit_date) values('zhangsan', '上海', '2020-01-01');
insert into footprint(username, city, visit_date) values('zhangsan', '上海', '2020-02-02');
insert into footprint(username, city, visit_date) values('lisi', '拉萨', '2016-12-20');

mysql> select * from footprint;
+----+----------+------+------------+
| id | username | city | visit_date |
+----+----------+------+------------+
|  1 | liusa    | 贵阳 | 2019-12-05 |
|  2 | liusa    | 贵阳 | 2020-01-15 |
|  3 | liusa    | 北京 | 2018-10-10 |
|  4 | zhangsan | 上海 | 2020-01-01 |
|  5 | zhangsan | 上海 | 2020-02-02 |
|  6 | lisi     | 拉萨 | 2016-12-20 |
+----+----------+------+------------+

对用户名去重
mysql> select distinct username from footprint;
+----------+
| username |
+----------+
| liusa    |
| zhangsan |
| lisi     |
+----------+
对城市去重
mysql> select distinct city from footprint;
+------+
| city |
+------+
| 贵阳 |
| 北京 |
| 上海 |
| 拉萨 |
+------+

对用户名,城市同时去重
mysql> select distinct username,city from footprint;
+----------+------+
| username | city |
+----------+------+
| liusa    | 贵阳 |
| liusa    | 北京 |
| zhangsan | 上海 |
| lisi     | 拉萨 |
+----------+------+



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值