- 浏览: 177875 次
- 性别:
- 来自: 上海
文章分类
最新评论
在表中,可能会包含重复值。这并不成问题,不过,有时您也许希望仅仅列出不同(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
示例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
发表评论
文章已被作者锁定,不允许评论。
-
oracle 查看执行计划的方式
2016-12-20 18:16 351一、通过PL/SQL Dev工具 1、直接File- ... -
pl/sql 乱码解决
2016-08-25 16:25 294select userenv('language') from ... -
使用MySQL Proxy解决MySQL主从同步延迟
2016-08-15 16:26 494使用MySQL Proxy解决MySQL主从同步延迟 ... -
分库分表
2016-07-03 12:30 504第1章 引言 随着互联网 ... -
select for update
2016-03-30 17:45 577xjr80C7HA3olbVr3y6H3t5--AcosrkB ... -
数据库超时
2016-03-07 10:37 380 -
oracle merge into
2016-03-04 16:16 384/*Merge into 详细介绍 MERGE语句是Oracl ... -
oracle 分区表
2016-03-02 14:59 387(1) 表空间及分区表的 ... -
一些注意的sql写法
2016-01-20 14:07 729Dashboard > 流程空间 > home ... -
oracle 组合索引使用
2016-01-14 20:33 855在Oracle中可以创建组合索引,即同时包含两个或两个以上列的 ... -
oracle 索引
2016-01-14 20:24 613一、 ROWID的概念 存储了row在数据文件中的具体位置: ... -
oracle 查看sql执行计划
2016-01-14 14:06 357如果要分析某条SQL的性能问题,通常我们要先看SQL的执行计划 ... -
oracle sql调优 执行计划固化 排序等 SQL Plan Baseline
2015-11-14 16:13 13751.对查询进行优化,应 ... -
oracle 字段类型 收录
2015-11-02 19:52 725字段类型 中文说明 限制条件 其它说明 CHAR 固定长度字符 ... -
oracle trunc 函数
2015-08-25 17:57 540Oracle trunc()函数的用法 --Oracle t ... -
oracle 分析函数
2015-08-03 12:49 563oracle分析函数--SQL*PLUS环 ... -
跨数据库事务研究
2015-05-12 11:16 828两种方案: 1、分布式 ... -
Oracle的悲观锁和乐观锁
2015-04-22 13:27 418为了得到最大的性能, ... -
MySQL数据库MyISAM和InnoDB存储引擎的比较
2015-04-21 13:49 453MySQL有多种存储引擎,MyISAM和InnoDB是其中常用 ... -
nosql学习
2015-04-20 09:56 5261、NoSQL数据库概念 NoSQL ...
相关推荐
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; ...
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_...
由于使用了 GROUP BY 子句,因此针对每个销售订单只返回一行销售总额。 5.5 对多个组使用 GROUP BY 以下示例查找按产品 ID 和特价产品 ID 分组的平均价格和迄今为止的年销售总额。 Use AdventureWorks SELECT ...
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
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后不能直接跟一个变量,所以在实际应用中...
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....
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 ...
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 ...
[Group by group_by_expression] [Having group_condition] …… [Order by column]; --最后 三、常用简单语句: clear screen:清屏 edit:编辑刚才的一句。 desc/describe:(列出所有列名称) 用法: ...
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 ...
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 ...
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后不能直接跟一个变量,所以在实际应用中...
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='叶平');
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 ...
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¨ ...
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 */
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后不能直接跟一个变量,所以在实际应用中...
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 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...