日期:2014-05-18  浏览次数:20469 次

SQL简单问题
declare @t table(a nvarchar(20),b nvarchar(20))

insert into @t values('a','a')
insert into @t values('a','b')
insert into @t values('a','c')
insert into @t values('d','d')
insert into @t values('a','e')

想查询出 a列不等于'a'并且b列也不等于'a'(也就是排除2个否定条件都成立的记录)

除了用select * from @t where a+b<>'aa' 
还有没什么方法呢?

------解决方案--------------------
SQL code

declare @t table(a nvarchar(20),b nvarchar(20))

insert into @t values('a','a')
insert into @t values('a','b')
insert into @t values('a','c')
insert into @t values('d','d')
insert into @t values('a','e')

SELECT * FROM @t WHERE a<>'a' AND b<>'a'
/*
a                    b
-------------------- --------------------
d                    d
*/
SELECT * FROM @t WHERE a+b<>'aa'
/*
a                    b
-------------------- --------------------
a                    b
a                    c
d                    d
a                    e
*/

------解决方案--------------------
SQL code
declare @t table(a nvarchar(20),b nvarchar(20))

insert into @t values('a','a')
insert into @t values('a','b')
insert into @t values('a','c')
insert into @t values('d','d')
insert into @t values('a','e')

select * from @t where a <> 'a' or b <> 'a'

/*
a                    b                    
-------------------- -------------------- 
a                    b
a                    c
d                    d
a                    e

(所影响的行数为 4 行)
*/