求这样的SQL语句?
表1中有字段a,值的格式:
id a
1 1
2 11,10
3 2,30,10
4 20
我想传入 1或2 的参数进去,只能搜索到id=1或id=3的记录,用like或charindex,也会搜索含有1或2的字符,其实我的搜索条件是以 ", "分隔来查询匹配记录
------解决方案--------------------declare @s nvarchar(10)
set @s = '1 '
select * from yTable
where charindex( ', ' + @s + ', ', ', ' + a + ', ') > 0
------解决方案--------------------id a
1 1
2 11,10
3 2,30,10
4 20
select * from tb where charindex( ',1, ' , ', '+a+ ', ')> 0
------解决方案--------------------CREATE TABLE tb(ID INT,A VARCHAR(20))
INSERT tb SELECT 1, 1
UNION ALL SELECT 2, '11,10 '
UNION ALL SELECT 3, '2,30,10 '
UNION ALL SELECT 4, '20 '
DECLARE @s VARCHAR(1000)
SET @s= '1,2 '
SET @s= 'SELECT V= ' + REPLACE(@s, ', ', ' UNION SELECT ')
EXEC( 'SELECT x.* FROM tb x INNER JOIN ( ' + @s + ')y ON CHARINDEX( ' ', ' ' + RTRIM(V) + ' ', ' ', ' ', ' ' + A + ' ', ' ')> 0 ')
DROP TABLE tb