mysql学习笔记,用distinct去除重复数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mysql学习笔记,用distinct去除重复数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2066字,纯文字阅读大概需要3分钟。
内容图文
大家好,我是天空之城,今天给大家带来,用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 | 拉萨 |
+----------+------+
内容总结
以上是互联网集市为您收集整理的mysql学习笔记,用distinct去除重复数据全部内容,希望文章能够帮你解决mysql学习笔记,用distinct去除重复数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。