使用SQL子查詢實現查找唯一值
作者:佚名
使用SQL子查詢,可以實現一些特定的功能,下面就為你介紹使用SQL子查詢實現查找唯一值的方法,供您參考。
下文為您介紹的是使用SQL子查詢,實現查找***值的目的,該方法供您參考學習,希望對您了解SQL子查詢有些幫助。
實現原理為:先取不與我相同的標識號的內容,而我的內容又不存在這些內容中,所以我的內容是***的.即即***值
SQL子查詢例子:在一個用戶表中取出***組的用戶
- if object_id('Test_Users') is not null
- drop table Test_Users
- go
- create table Test_Users(AutoID int identity(1,1) primary key,UserGroupID int,UserName varchar(50))
- go
- set xact_abort on
- begin tran
- insert into Test_Users(UserGroupID,UserName)
- select 2,'aa' union
- select 2,'bb' union
- select 3,'cc' union
- select 1,'Admin' union
- select 3,'ff' union
- select 2,'pp'
- commit tran
- go
- select * from Test_Users a
- where UserGroupID not in
- (select UserGroupID from Test_Users where AutoID<>a.AutoID)
---這樣找出的結果是
- AutoID UserGroupID UserName
- ----------- ----------- --------------------------------------------------
- 1 1 Admin
- (所影響的行數為 1 行)
【編輯推薦】
責任編輯:段燃
來源:
互聯網