首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

不包含某些值的SQL语句应该如何写

2012-04-06 
不包含某些值的SQL语句应该怎么写table1如下bbb(字段)2351想要求出21下面的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)


[解决办法]

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)godeclare @arr varchar(10) set @arr='3,5'select * from table1 where charindex(','+cast(bbb as varchar) + ',' , ',' + @arr + ',') = 0/*bbb         ----------- 21(所影响的行数为 2 行)*/select * from table1 where ',' + @arr + ',' not like '%,'+cast(bbb as varchar) + ',%'/*bbb         ----------- 21(所影响的行数为 2 行)*/drop table table1 

热点排行