`

sql distinctt group by 分析

 
阅读更多
在表中,可能会包含重复值。这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值。关键词 distinct用于返回唯一不同的值。

示例1
select distinct name from A
示例2

select distinct name, id from A

实际上是根据“name+id”来去重,distinct同时作用在了name和id上

示例3:统计
select count(distinct name) from A;
select count(distinct name, id) from A; mysql 支持的
示例4
select id, distinct name from A;   --会提示错误,因为distinct必须放在开头

网上的一些测试结果;
加了索引之后 distinct 比没加索引的 distinct 快了 107倍。
加了索引之后 group by 比没加索引的 group by 快了 43倍。
再来对比 :distinct  和 group by
不管是加不加索引 group by 都比 distinct 快。因此使用的时候建议选 group by
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    T-SQL高级查询

    select count(*), (sex * id) new from student where id > 2 group by sex * id order by sex * id; --group by all 所有分组 按照年龄分组,是所有的年龄 select count(*), age from student group by all age; ...

    SQLQuery5.sql

    from score a left join ( select a.s_score from Score a group by a.s_score having count(a.s_score)>1 and count(distinct a.c_id) > 1)tt on tt.s_score=a.s_score group by a.s_id select distinct a.s_...

    SQL示例大全.pdf

    由于使用了 GROUP BY 子句,因此针对每个销售订单只返回一行销售总额。 5.5 对多个组使用 GROUP BY 以下示例查找按产品 ID 和特价产品 ID 分组的平均价格和迄今为止的年销售总额。 Use AdventureWorks SELECT ...

    行转列sql_server

    sql server 用于行转列,省得各位去找语句,select to_char(wm_concat('''' || valuelabel || '''')) name from (select distinct t.... group by t.uhid, t.form_name, valuelabel) pivot(max(valuedata) valuedata

    经典SQL语句大全

    set @sql=’select top’+str(@end-@start+1)+’+from T where rid not in(select top’+str(@str-1)+’Rid from T where Rid>-1)’ exec sp_executesql @sql 注意:在top后不能直接跟一个变量,所以在实际应用中...

    LINQ_to_SQL语法及实例大全

    6. 带GroupBy形式 22 LINQ to SQL语句(6)之Group By/Having 23 Group By/Having操作符 23 1.简单形式: 23 2.Select匿名类 : 24 3.最大 值 25 4.最小 值 26 5.平均 值 26 6.求和 26 7.计数 27 8.带条件计数 27 9....

    50个常用SQL语句,很好

    where S# in (select S# from SC ,Course ,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='叶平' group by S# having count(SC.C#)=(select count(C#) from Course,Teacher where ...

    SQL Server 2008编程入门经典(第3版)

    3.1.4 使用GROUPBY子句聚合数据 3.1.5 使用HAVING子句给分组设置条件 3.1.6 使用FORXML子句输出XML 3.1.7 通过OPTION子句利用提示 3.1.8 DISTINCT和ALL谓词 3.2 使用INSERT语句添加数据 3.2.1 多行插入 3.2.2 ...

    2009达内SQL学习笔记

    [Group by group_by_expression] [Having group_condition] …… [Order by column]; --最后 三、常用简单语句: clear screen:清屏 edit:编辑刚才的一句。 desc/describe:(列出所有列名称) 用法: ...

    SQL.Server.2008编程入门经典(第3版).part2.rar

    3.1.4 使用GROUPBY子句聚合数据 3.1.5 使用HAVING子句给分组设置条件 3.1.6 使用FORXML子句输出XML 3.1.7 通过OPTION子句利用提示 3.1.8 DISTINCT和ALL谓词 3.2 使用INSERT语句添加数据 3.2.1 多行插入 3.2.2 ...

    SQL.Server.2008编程入门经典(第3版).part1.rar

    3.1.4 使用GROUPBY子句聚合数据 3.1.5 使用HAVING子句给分组设置条件 3.1.6 使用FORXML子句输出XML 3.1.7 通过OPTION子句利用提示 3.1.8 DISTINCT和ALL谓词 3.2 使用INSERT语句添加数据 3.2.1 多行插入 3.2.2 ...

    超实用sql语句

    set @sql=’select top’+str(@end-@start+1)+’+from T where rid not in(select top’+str(@str-1)+’Rid from T where Rid>-1)’ exec sp_executesql @sql 注意:在top后不能直接跟一个变量,所以在实际应用中...

    sql面试题- (学生表_课程表_成绩表_教师表)

    1、查询“001”课程比“002”课程成绩高的所有学生的学号;... where S# not in (select distinct( SC.S#) from SC,Course,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='叶平');

    精通sql结构化查询语句

    6.2.4 使用别名 6.3 排序与分组 6.3.1 升序排序与降序排序 6.3.2 多列字段排序 6.3.3 使用GROUPBY子句对查询结果进行分组 6.3.4 HAVING子句的应用 6.4 条件查询 6.4.1 WHERE单条件语句查询 6.4.2 运算符查询 6.4.3 ...

    MYSSQL_MSS_ORACLE经典SQL.pdf

    GROUP BY t.C# ORDER BY 100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) DESC 20、--查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001),马克思(002),OO¨ ...

    sql游标实例,更新行

    USE AA if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tmp_Last]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[tmp_Last] ...Group by cOutBillNo */

    sql经典语句一部分

    set @sql=’select top’+str(@end-@start+1)+’+from T where rid not in(select top’+str(@str-1)+’Rid from T where Rid>-1)’ exec sp_executesql @sql 注意:在top后不能直接跟一个变量,所以在实际应用中...

    数据库操作语句大全(sql)

    set @sql=’select top’+str(@end-@start+1)+’+from T where rid not in(select top’+str(@str-1)+’Rid from T where Rid>-1)’ exec sp_executesql @sql 注意:在top后不能直接跟一个变量,所以在实际应用...

    微软内部资料-SQL性能优化5

    SQL Server keeps track of which pages belong to a table or index by using IAM pages. If there is no clustered index, there is a sysindexes row for the table with an indid value of 0, and that row will...

Global site tag (gtag.js) - Google Analytics