mysql distinct 去重
在使用MySQL时,有时需要查询出某个字段不重复的记录,这时可以使用mysql提供的distinct这个关键字来过滤重复的记录,但是实际中我们往往用distinct来返回不重复字段的条数(count(distinct id)),其原因是distinct只能返回他的目标字段,而无法返回其他字段,例如有如下表user:
用distinct来返回不重复的用户名:select distinct name from user;,结果为:
这样只把不重复的用户名查询出来了,但是用户的id,并没有被查询出来:select distinct name,id from user;,这样的结果为:
distinct name,id 这样的mysql 会认为要过滤掉name和id两个字段都重复的记录,如果sql这样写:select id,distinct name from user,这样mysql会报错,因为distinct必须放在要查询字段的开头。
所以一般distinct用来查询不重复记录的条数。
如果要查询不重复的记录,有时候可以用group by :
select id,name from user group by name;
mysql distinct 去重的更多相关文章
- mysql 查询去重 distinct
mysql 查询去重 distinct 待完善内容..
- mysql distinct、group_concat
mysql distinct语句用于查询多条不重复记录值(去重.过滤多余的重复记录) distinct同时作用了两个字段或者两个以上的字段,必须得作用了的字段都相同的才被排除.如果想让单个列" ...
- Mysql DISTINCT问题
问题描述 因为要设计一个数据库表,进行一个倒序去重的操作. 例如: id Name 1 B 2 A 3 A 4 C 5 C 6 B 场景:例如说我们需要得到一个用户的搜索记录,那么肯定不会仅仅根据时间 ...
- 转 mysql distinct函数 与 免密码登录 与 查看表的结构
#########sample 1 mysql中去重 distinct 用法 在使用MySQL时,有时需要查询出某个字段不重复的记录,这时可以使用mysql提供的distinct这个关键字来过滤重 ...
- MySQL的去重+列的表达式
MySQL的去重+列的表达式 1. 去重 作用:去除SELECT查询出来的结果中重复的数据,重复的数据只显示一条. SELECT * FROM `repeat_num` ...
- mysql distinct跟group by性能
mysql distinct和group by性能 1,测试前的准备 //准备一张测试表 mysql> CREATE TABLE `test_test` ( -> `id` int ...
- Linq 中的distinct去重
Linq的Distinct和T-Sql的distinct一样,可以将重复的结果集去重注意: 1 distinct去重记录要求每个字段都重复时,才算重复对象,这与sql一样2 distinct语句可以和 ...
- mysql distinct field1,field2,field3, .... from table
mysql distinct field1,field2,field3, .... from table 我们知道 这样的sql可以去掉重复项 (field1的重复项); select distinc ...
- 存储过程系列三:根据表别名方式distinct去重插入
1.根据表别名方式distinct去重插入 insert into GG_XKZ_YLQXSCXKESL_SCDZ ( bzj, xkzid, sqid, jtdz, szsf, ...
随机推荐
- 最好的8个 Java RESTful 框架
原文出处: colobu 过去的每一年,涌现出越来越多的Java框架.就像JavaScript,每个人都认为他们知道一个好的框架的功能应该是怎么样的.连我的老祖母现在也使用 一个我从来没有听说过而且可 ...
- Linux - CentOS7上的时间同步
1. 时区的概念 1.1 时区简介 地球是自西向东自转,东边比西边先看到太阳,东边的时间也比西边的早.东边时刻与西边时刻的差值不仅要以时计,而且还要以分和秒来计算,这给人们带来不便.所以为了克服时间上 ...
- redis命令大全参考手册
redis功能强大,支持数据类型丰富,以下是redis操作命令大全,基本上涵盖了redis所有的命令,并附有解释说明,大家可以收藏.参考,你一定要知道的是:redis的key名要区分大小写,在redi ...
- OpenSCAD 大白
$fn=10; module bag_bar(rr1,rr2,d) { rotate_extrude() difference() { hull() //hull() fast in 2D, no g ...
- the security of smart contract- 1
https://blog.zeppelin.solutions/the-hitchhikers-guide-to-smart-contracts-in-ethereum-848f08001f05 这个 ...
- ethereum/EIPs-160 EXP cost increase
eip title author type category status created 160 EXP cost increase Vitalik Buterin Standards Track ...
- warning: ISO C++ forbids converting a string constant to 'char*'
第1种字符串赋值方式: char * fileName="./2017-09-02-10-34-10.xml";//这一种字符串赋值方式已经被ISO禁止了 第2种字符串赋值方式: ...
- 博客搬家了qwq
呃,其实也不是搬家了,应该算是逐渐过渡qwq \[\color{skyblue}{Orchid} \color{purple}{any}\] 好的,我在学校里并不可以用Hexo,因为deploy总是挂 ...
- mysql 修改字段
alter table dev_instant_video modify column content varchar(2500)alter table dev_instant_video_info ...
- (转)对一个deb包的解压、修改、重新打包全过程方法
转自:https://blog.csdn.net/yygydjkthh/article/details/36695243 Reference: http://www.debian.org/doc/ma ...