ojuju10 数据库中的字段怎么分我尝试了一个 SELECT * FROM Product_VIEW1 where '103' in (AttributeValueid) and '101' in (AttributeValueid) and '107' in (AttributeValueid)但是不行。
我只是举个例子你是做搜索功能吧把类似于“103,101,107”处理成 like "%101%103%107%" 形式的查询条件传入就可以了
declare @a char(100),@b char(100),@c char(100) select @a='103',@b='101',@c='107' select * from tb where fd like'%' +@a+'%' and fd like '%'+@b+'%' and fd like'%'+@c+'%'
youngerch 你的方法不太适用的确是用在搜索 是搜索商品的 属性值
create table tb(s varchar(20)) insert into tb select '101,102,103,107' insert into tb select '102,103,107'declare @sql varchar(8000) set @sql='103,101,107' set @sql='select * into ## from (select '+replace(@sql,',',' as id union all select ')+')tp' exec(@sql)declare @where varchar(8000) select @where=isnull(@where+' and ','')+'charindex('''+ltrim(id)+''',s)>0' from ## exec('select * from tb where '+@where)101,102,103,107
Create table tt(name varchar(20)) insert into tt select '101, 102, 103, 107' Create function f_tt(@v varchar(20)) returns @tb table(name varchar(10)) as begin while charindex(',',@v)>0 begin insert into @tb select substring(@v,1,charindex(',',@v)-1) set @v=stuff(@v,1,charindex(',',@v),'') end insert into @tb select @v return enddeclare @v varchar(20)set @v='103,101,107'select * from tt a,dbo.f_tt(@v) b where charindex(','+b.name+',',','+a.name+',')>0
create table tb(s varchar(20)) insert into tb select '101,102,103,107' insert into tb select '102,103,107' insert into tb select '103,101,1077'declare @sql varchar(8000) set @sql='103,101,107' set @sql='select * into ## from (select '+replace(@sql,',',' as id union all select ')+')tp' exec(@sql)declare @where varchar(8000) select @where=isnull(@where+' and ','')+'charindex('','+ltrim(id)+','','',''+s+'','')>0' from ## exec('select * from tb where '+@where)101,102,103,107精确了
select * from table where columnName like '%101%' and columnName like '%102%' and columnName like '%107%'
1、位数是不定的
2、位置可能颠倒
@split VARCHAR(10)
SELECT @para = '117,118,123,',@split = ''
SELECT CHARINDEX(',',@para)DECLARE @t TABLE(Id INT)
WHILE CHARINDEX(',',@para)>0
BEGIN
SELECT @split = LEFT(@para, CHARINDEX(',', @para)-1), @para = RIGHT(@para, LEN(@para)-CHARINDEX(',', @para))
INSERT INTO @t
SELECT @split
ENDSELECT * FROM @t
--result
-----------
--4
--
--(1 row(s) affected)
--
--(1 row(s) affected)
--
--(1 row(s) affected)
--
--(1 row(s) affected)
--
--Id
-------------
--117
--118
--123
--
--(3 row(s) affected)
--最后你在用这个@t与你的主表做连接查询。
--具体你自己在改改
SELECT * FROM Product_VIEW1 where '103' in (AttributeValueid) and '101' in (AttributeValueid) and '107' in (AttributeValueid)但是不行。
我只是举个例子你是做搜索功能吧把类似于“103,101,107”处理成 like "%101%103%107%" 形式的查询条件传入就可以了
这个时候分开 效率感觉不高 还不如 我再建立一个表来保存 里面的 103 101 了
select @a='103',@b='101',@c='107'
select * from tb where fd like'%' +@a+'%' and fd like '%'+@b+'%' and fd like'%'+@c+'%'
insert into tb select '101,102,103,107'
insert into tb select '102,103,107'declare @sql varchar(8000)
set @sql='103,101,107'
set @sql='select * into ## from (select '+replace(@sql,',',' as id union all select ')+')tp'
exec(@sql)declare @where varchar(8000)
select @where=isnull(@where+' and ','')+'charindex('''+ltrim(id)+''',s)>0'
from ##
exec('select * from tb where '+@where)101,102,103,107
例如 我要寻找 101 SQL:%101%
查出来结果 包含11111101,101223322 等等
我要需要精确查询
insert into tt select '101, 102, 103, 107' Create function f_tt(@v varchar(20))
returns @tb table(name varchar(10))
as
begin
while charindex(',',@v)>0
begin
insert into @tb select substring(@v,1,charindex(',',@v)-1)
set @v=stuff(@v,1,charindex(',',@v),'')
end
insert into @tb select @v
return
enddeclare @v varchar(20)set @v='103,101,107'select * from tt a,dbo.f_tt(@v) b
where charindex(','+b.name+',',','+a.name+',')>0
insert into tb select '101,102,103,107'
insert into tb select '102,103,107'
insert into tb select '103,101,1077'declare @sql varchar(8000)
set @sql='103,101,107'
set @sql='select * into ## from (select '+replace(@sql,',',' as id union all select ')+')tp'
exec(@sql)declare @where varchar(8000)
select @where=isnull(@where+' and ','')+'charindex('','+ltrim(id)+','','',''+s+'','')>0'
from ##
exec('select * from tb where '+@where)101,102,103,107精确了