distinct和group by的区别

如题所述

这两者本质上应该没有可比性,distinct 取出唯一列,group by 是分组,但有时候在优化的时候,在没有聚合函数的时候,他们查出来的结果也一样。

一、group by

英 [ɡru:p bai]   美 [ɡrup baɪ]  

[计][WIN]分组依据

拓展资料

1、This operator also serves as input to the Group By operator.

该操作符还充当Group By操作符的输入。

2、Complete the following steps to create and program the Group By operator. 

完成以下步骤,创建Group By操作符并对它进行配置。

3、With the appropriate access path, the ORDER BY or GROUP BY requirement can be met without sorting. 

使用恰当的访问路径,无需排序即可满足ORDER BY或GROUP BY需求。

4、Drag it to the right of the Group By operator. 

将它拖放到Group By操作符的右边。

二、distinct

英 [dɪˈstɪŋkt]   美 [dɪˈstɪŋkt]  

adj.明显的,清楚的;卓越的,不寻常的;有区别的;确切的

拓展资料

1、I have distinct memories of him in his last years. 

我清楚地记得他最后几年的情况。

2、Another Cup marathon between the two sides is now a distinct possibility. 

双方很可能再进行一场马拉松式的优胜杯比赛。

3、The photograph showed a distinct image. 

照片显出了明晰的影像。

4、I want a distinct answer to my question. 

我要求对我的问题作出明确的答复。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-09-29
他们的功能基本上是不一样的。
distinct消除重复行。
group by是分组语句。
举例来说可能方便一点。
A表id numa 1b 2c 3a 4c 7d 3e 5
如果只选出id列,用distinct和group by 一样的。
select distinct(id) from A;idabcde;select id from A group by id;idabcde;不同之处可能在于group by有排序功能。
但是如果需要加上另一列num,结果不同。
group by 是分组语句,如果用
select id,num from A group by id,num;
这样的结果在本例中与不加group by是一样的,因为num各个不同。
但是如果select id,num from A group by id;
注意该语句是错误语句,因为num没有使用聚组函数,例如:sum(求和),avg(求平均数)
select id,sum(num) from A group by id;
id sum(num)a 5b 2c 10d 3e 5
用distinct不显示重复的行。
在本例中select distinct id,num from A;的结果也和不加distinct一致。
因为id,num没有重复的行,而不是只看id。
group by 功能更强大一些,另外推荐使用group by。
因为distinct会导致全表扫描,而group by如果索引建的
恰当的话,会有性能上的提高。本回答被提问者采纳
第2个回答  2017-09-29
distinct:分出不一样的
group by:把一样的分组

相关了解……

你可能感兴趣的内容

大家正在搜

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网