select * from b where charindex(',15,',','+tt+',')>0
select a.*,b.tt from a ,(select tt,tt1=','+tt+',' from b) c where charindex(','+cast(a.types as varchar)+',',c.tt1)>0
--测试数据 create table A(types int) insert A select 15create table B(tt varchar(20)) insert B select '1' union all select '15' union all select '22' union all select '13,45' union all select '18,15' union all select '112,556,45,15' go--查询 select * from a,b where charindex(','+cast(a.types as varchar)+',',','+b.tt+',')>0 go--删除测试 drop table a,b/*--测试结果types tt ----------- -------------------- 15 15 15 18,15 15 112,556,45,15(所影响的行数为 3 行) --*/
to: zjcxc(邹建) 如果我不是和A表中的字段比较,而是和一个变量比较,这样写怎么不对? select * from a,b where charindex(','+cast(变量名称 as varchar)+',',','+b.tt+',')>0怎么会不对 异常详细信息: System.Data.SqlClient.SqlException: 列名 'types' 无效。 它把这个变量当作列名处理,是怎么回事?
你在那里引用了 types ?
--晕,看来你不会定义变量declare @types int set @types=15 select * from b where charindex(','+cast(@types as varchar)+',',','+tt+',')>0
,(select tt,tt1=','+tt+',' from b) c
where charindex(','+cast(a.types as varchar)+',',c.tt1)>0
create table A(types int)
insert A select 15create table B(tt varchar(20))
insert B select '1'
union all select '15'
union all select '22'
union all select '13,45'
union all select '18,15'
union all select '112,556,45,15'
go--查询
select * from a,b
where charindex(','+cast(a.types as varchar)+',',','+b.tt+',')>0
go--删除测试
drop table a,b/*--测试结果types tt
----------- --------------------
15 15
15 18,15
15 112,556,45,15(所影响的行数为 3 行)
--*/
如果我不是和A表中的字段比较,而是和一个变量比较,这样写怎么不对?
select * from a,b
where charindex(','+cast(变量名称 as varchar)+',',','+b.tt+',')>0怎么会不对
异常详细信息: System.Data.SqlClient.SqlException: 列名 'types' 无效。
它把这个变量当作列名处理,是怎么回事?
set @types=15
select * from b
where charindex(','+cast(@types as varchar)+',',','+tt+',')>0