sql select distinct top 关键字简介


distinct top 关键字功能简介

distinct top 都属于select查询中的可选关键字。
distinct:  功能是,当表中存在重复值时,我们可以只展示出不重复的列值,或不重复的组合列值
top:    功能是,显示表前多少行,当我们不指定排序时,系统会按照默认排序展示出相关数据,否则按照指定规则重新排序


distinct top 关键字应用

distinct top 关键字语法

select distinct 列名 from 表名
select distinct 列名1,列名2 from 表名

select top 列名1,列名2,列名3 ... from 表名

select top 列名1,列名2,列名3 ... from 表名 order by [列名] desc 


/* 1 建表 并插入数据*/
create table A(keyId int,info varchar(60), writeDate datetime)
insert into A values
(1,'a','2017-1-1'),(2,'b','2017-1-2'),(3,'c','2017-1-3'),
(11,'a','2017-2-1'),(12,'b','2017-2-2'),(13,'c','2017-2-3'),
(21,'a','2017-3-1'),(22,'b','2017-3-2'),(23,'c','2017-3-3'),
(31,'a','2017-4-1'),(32,'b','2017-4-2'),(33,'c','2017-4-3'),
(41,'a','2017-5-1'),(42,'b','2017-5-2'),(43,'c','2017-5-3')

/*2 distinct 用法*/
select distinct info from A 

select distinct keyId,info from A

/*3 top 用法*/

select top 5 keyId,info,writeDate from A

select top 5 keyId,info,writeDate from A order by writeDate desc 


/*4 清理表*/

truncate table [A]
drop table [A]



相关阅读:

sql select
sql简介
sql 删除重复数据
如果查询MSSQL中重复列(group by having count)