使用SQL子查询实现查找唯一值
				
									
					
					
						 | 
						
							
							admin 
							
							
								2011年5月4日 17:26
								本文热度 4190
							
							 
						 | 
					
					
				 
				
下文为您介绍的是使用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 行)  
 
 
该文章在 2011/5/4 17:26:34 编辑过