现在一条SQL查询语句:select * from table where fieldA in ('13,12,11,10')其中关键字IN的左边fieldA字段也是存储与“,”分隔的数组,应该可以处理匹配,得到结果集。又或者这里的关键字已经不应该再用到“IN”呢?如何我想只是取出fieldA数组中的一值出来匹配,是否可以实现?请高手指教。在此谢过
這個效果??Create Table TEST (fieldA Varchar(100)) Insert TEST Select '13,12' Union All Select '12,11' Union All Select '12,10' GO select * from TEST where CharIndex(','+fieldA+',',','+ ('13,12,11,10')+',') >0 GO Drop Table TEST --Result /* fieldA 13,12 12,11 */
我這裡是做一個測試環境的,模擬你的表的情況。假設我的TEST表就是你要查詢的表,fieldA就是你要查詢的字段。下面的@S就是用戶傳入的字符串。Create Table TEST (fieldA Varchar(100)) Insert TEST Select '13,12' Union All Select '12,11' Union All Select '12,10' GO Declare @S Varchar(100) Set @S='13,12,11,10' select * from TEST where CharIndex(','+fieldA+',',','+ @S+',') >0 GO Drop Table TEST --Result /* fieldA 13,12 12,11 */
您的语句是否先拆分原表字段,然后再建立一个临时的记录表呢?那么按照您的思路这条SQL查询语句是否可以写为: select * from table where CharIndex(','+fieldA+',',','+ ('13,12,11,10')+',') >0 ?
wendye() ( ) 信誉:100 2006-05-29 15:24:00 得分: 0
您的语句是否先拆分原表字段,然后再建立一个临时的记录表呢?那么按照您的思路这条SQL查询语句是否可以写为: select * from table where CharIndex(','+fieldA+',',','+ ('13,12,11,10')+',') >0 ? ------------------------------------------------ 拆分表?!不必啊。你表中的數據“13,12”不是這種格式的嗎?? '13,12,11,10'可改為你傳入的變量。
(fieldA Varchar(100))
Insert TEST Select '13,12'
Union All Select '12,11'
Union All Select '12,10'
GO
select * from TEST where CharIndex(','+fieldA+',',','+ ('13,12,11,10')+',') >0
GO
Drop Table TEST
--Result
/*
fieldA
13,12
12,11
*/
小弟先测试一下
(fieldA Varchar(100))
Insert TEST Select '13,12'
Union All Select '12,11'
Union All Select '12,10'
GO
Declare @S Varchar(100)
Set @S='13,12,11,10'
select * from TEST where CharIndex(','+fieldA+',',','+ @S+',') >0
GO
Drop Table TEST
--Result
/*
fieldA
13,12
12,11
*/
select * from table where CharIndex(','+fieldA+',',','+ ('13,12,11,10')+',') >0
?
您的语句是否先拆分原表字段,然后再建立一个临时的记录表呢?那么按照您的思路这条SQL查询语句是否可以写为:
select * from table where CharIndex(','+fieldA+',',','+ ('13,12,11,10')+',') >0
?
------------------------------------------------
拆分表?!不必啊。你表中的數據“13,12”不是這種格式的嗎??
'13,12,11,10'可改為你傳入的變量。
是整句的重点。我明白思路了,谢谢!