不包含某些值的SQL语句应该如何写
不包含某些值的SQL语句应该怎么写
table1如下
bbb(字段)
2
3
5
1
想要求出
2
1
下面的SQL表达式是不对的。但这种写法应该怎么写呢。ARR必须是用变量的.希望高手指点
declare @arr varchar(10) set @arr='3,5'
select * from table1 where bbb not in (@arr)
------解决方案--------------------
table1如下
bbb(字段)
2
3
5
1
想要求出
2
1
下面的SQL表达式是不对的。但这种写法应该怎么写呢。ARR必须是用变量的.希望高手指点
declare @arr varchar(10) set @arr='3,5'
select * from table1 where bbb not in (@arr)
------解决方案--------------------
- SQL code
create table table1(bbb int) insert into table1 values(2) insert into table1 values(3) insert into table1 values(5) insert into table1 values(1) go declare @arr varchar(10) set @arr='3,5' select * from table1 where charindex(','+cast(bbb as varchar) + ',' , ',' + @arr + ',') = 0 /* bbb ----------- 2 1 (所影响的行数为 2 行) */ select * from table1 where ',' + @arr + ',' not like '%,'+cast(bbb as varchar) + ',%' /* bbb ----------- 2 1 (所影响的行数为 2 行) */ drop table table1