distinct 与 group by 去重】的更多相关文章

mysql中常用去重复数据的方法是使用 distinct  或者group by ,以上2种均能实现,但2者也有不同的地方. distinct 特点: 如:select  distinct   name, sex,from tb_students  这个sql的语法中,查询 tb_students  表中 name, sex,并去除名字和性别都重复的学生: 1.distinct 只能放在查询字段的最前面,不能放在查询字段的中间或者后面. 备注:select   sex,distinct   na…
例如下表格:表名:fruit id Name Price Num 1 西瓜 10 2 2 西瓜 11 2 3 香蕉 10 3 4 桃子 10 2 当我想获取Name不重复的数据,结果如下 id Name Price Num 1 西瓜 10 2 3 香蕉 10 3 4 桃子 10 2 如果查询时用 distinct,则无效果,只能用group by. select * from fruit where id in (select min(id) from fruit  group by Name)…
转自:http://blog.csdn.net/helencoder/article/details/50328629 近期项目中,遇到数据表去重要求,对于ThinkPHP的去重有了更加准确的认识和体会. 两种去重方式: $test_data = M('hot'); //实例化数据表 $data = $test_data->Distinct(true)->field('descriprion')->order('description desc')->select(); //利用d…
遇到一个需求,要去重查出某张表的字段一和字段二,但是查出来的结果要按照表中记录的创建时间排序. 于是,第一时间就想到了使用distinct这个去重专用语法了: select distinct col1, col2 from table1 order by create_date; 嗯,自我感觉良好,一运行,抛出了异常,因为select选出的结果中根本没有create_date这个字段,还用这个字段排序,那不就报错了吗,于是改为: select distinct col1, col2, creat…
使用distinct 和group by都可以实现数据去重. select distinct 字段 group by 一般放在where条件后…
44 答疑(三) Join的写法 35节介绍了join执行顺序,加了straight_join,两个问题: --1 如果用left join,左边的表一定是驱动表吗 --2 如果两个表的join包含多个条件的等值匹配,是都要写到on里面呢,还是只把一个写到on,把其他的条件写到where部分? create table a(f1 int, f2 int, index(f1))engine=innodb; create table b(f1 int, f2 int)engine=innodb; ,…
distinct和group by 是一样的,查询去重,只能是全部重复的,也可以理解为针对单例,因为一行有一个字段不一样,他们就会认为这两行内容是不重复的.但是使用row_number()over这个函数就可以针对全部字段,完全重复还是部分重复都可以通过这个函数查找出来,因为它自身有分组的功能.以下就是具体代码:…
MySQL中distinct和group by性能比较[转] 之前看了网上的一些测试,感觉不是很准确,今天亲自测试了一番.得出了结论(仅在个人计算机上测试,可能不全面,仅供参考) 测试过程: 准备一张测试表 1 CREATE TABLE `test_test` ( 2 `id` int(11) NOT NULL auto_increment, 3 `num` int(11) NOT NULL default '0', 4 PRIMARY KEY (`id`) 5 ) ENGINE=MyISAM…
mysql distinct和group by性能   1,测试前的准备 //准备一张测试表 mysql> CREATE TABLE `test_test` ( ->   `id` int(11) NOT NULL auto_increment, ->   `num` int(11) NOT NULL default '0', ->   PRIMARY KEY  (`id`) -> ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCR…
这两者本质上应该没有可比性,distinct 取出唯一列,group by 是分组,但有时候在优化的时候,在没有聚合函数的时候,他们查出来的结果也一样. 举例来说可能方便一点. A表 id num a 1 b 2 c 3 a 4 c 7 d 3 e 5 如果只选出id列,用distinct和group by 一样的. select distinct(id) from A; id a b c d e; select id from A group by id; id a b c d e; 不同之处…