常用的SQL 查找语句

1、NEWID()另外一个应用是在Select出记录时随即选出N条记录
比如:Select top N * from table order by newid()

 

2、获得数据库中Name字段重复的行

a,在A表中存在一个字段“name”, 而且不同记录之间的“name”值有可能会相同,现在就是需要查询出在该表中的各记录之间,“name”值存在重复的项;Select Name,Count(*) From A Group By Name Having Count(*) > 1

如果还查性别也相同大则如下:
Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1

b, 查找表中多余的重复记录(多个字段)
select * from table1 a where (a.peopleId,a.seq) in (select peopleId,seq from table1 group by peopleId,seq having count(*) > 1)


 3、改变字符窜的名称

Select sex= case sex when 'nan' then 'nv' else 'nan' end from tab

 

4,获得31到40的记录,

select top 10 * from tab where id not in(select top 30 * from tab  order by id desc ))

 

5、获得不重复的行

a、select distinct top 10  [name]  from tab  (distinct一定要方正top前面,即先不重复再取前10个)

b、如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除
select distinct * into #Tmp from tableName
drop table tableName
select * into tableName from #Tmp
drop table #Tmp
(发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。)

 

6.查询ID重复的行
select * from table where id in ( select id from table group by id having count(id) > 1 )

 

7,这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下
假设有重复的字段为Name,Address,要求得到这两个字段唯一的结果集
select identity(int,1,1) as autoID, * into #Tmp from tableName select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID
select * from #Tmp where autoID in(select autoID from #tmp2)
最后一个select即得到了Name,Address不重复的结果集(但多了一个autoID字段,实际写时可以写在select子句中省去此列)

8,删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,不留有rowid最小的记录
delete from people where peopleId in (select peopleId from people group by peopleId   having count(peopleId) > 1) and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)

10、group by方法

查数据:
  select count(num), max(name) from student --列出重复的记录数,并列出他的name属性
  group by num
  having count(num) >1 --按num分组后找出表中num列重复,即出现次数大于一次
删数据:
  delete from student
  group by num
  having count(num) >1
  这样的话就把所有重复的都删除了。

11、四表联查问题:
select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....

 

12、说明:日程安排提前五分钟提醒

 select * from 日程安排 where datediff('minute',f开始时间,getdate())>5

 

13、显示文章、提交人和最后回复时间
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b

14、说明:列出数据库里所有的表名
select name from sysobjects where type='U' // U代表用户

15、说明:列出表里的所有的列名
select name from syscolumns where id=object_id('TableName')

16、说明:列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重选择,类似select 中的case。
select type,sum(case vender when 'A' then pcs else 0 end),sum(case vender when 'C' then pcs else 0 end),sum(case vender when 'B' then pcs else 0 end) FROM tablename group by type

 

17:获取某一个表的所有字段
select name from syscolumns where id=object_id('表名')

18,连接远程/局域网数据(openrowset/openquery/opendatasource)

--1、openrowset

--查询示例

select * from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)

--生成本地表

select * into 表 from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)

 

--把本地表导入远程表

insert openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)

select *from 本地表

--更新本地表

update b  set b.列A=a.列A  from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)as a inner join 本地表 b  on a.column1=b.column1

--openquery用法需要创建一个连接

--首先创建一个连接创建链接服务器

exec sp_addlinkedserver   'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 '

--查询

select * FROM openquery(ITSV,  'SELECT *  FROM 数据库.dbo.表名 ')

--把本地表导入远程表

insert openquery(ITSV,  'SELECT *  FROM 数据库.dbo.表名 ')

select * from 本地表

--更新本地表

update b set b.列B=a.列B FROM openquery(ITSV,  'SELECT * FROM 数据库.dbo.表名 ') as a inner join 本地表 b on a.列A=b.列A

 

--3、opendatasource/openrowset

SELECT   * FROM   opendatasource( 'SQLOLEDB ',  'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ' ).test.dbo.roy_ta

--把本地表导入远程表

insert opendatasource( 'SQLOLEDB ',  'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ').数据库.dbo.表名

select * from 本地表  例子:select [name] from syscolumns where id in (select id from sysobjects where type = 'u' and name = '表名')

posted @ 2012-03-10 09:59  kevin655  阅读(520)  评论(0编辑  收藏  举报