declare @table table (id int,name varchar(1)) insert into @table select 1,'A' union all select 1,'B' union all select 2,'A' union all select 3,'B'declare @tj varchar(6);set @tj='A,B'if(charindex(',',@tj)>0) select max(id) from(select id from @table where name='A' union all select id from @table where name='B') aa group by id having(count(*)>1) else select max(id) from @table where name=@tj
declare @tj varchar(6);set @tj='A,B'if(charindex(',',@tj)>0) select max(id) from(select id from @table where name='A' intersect select id from @table where name='B') aa else select max(id) from @table where name=@tj改了下1楼的
换个思路…… DECLARE @tb TABLE (id INT,[name] VARCHAR(10)) INSERT @tb SELECT 1,'A' UNION ALL SELECT 1,'B' UNION ALL SELECT 2,'A' UNION ALL SELECT 3,'B';SELECT * FROM ( SELECT DISTINCT b.id , STUFF(( SELECT ',' + a.[name] FROM @tb a WHERE a.id = b.id FOR XML PATH('') ), 1, 1, '') AS [name] FROM @tb b ) c WHERE [name] = '查询条件'
declare @table table (id int,name varchar(1))
insert into @table
select 1,'A' union all
select 1,'B' union all
select 2,'A' union all
select 3,'B'declare @tj varchar(6);set @tj='A,B'if(charindex(',',@tj)>0)
select max(id) from(select id from @table where name='A' union all
select id from @table where name='B') aa group by id having(count(*)>1)
else
select max(id) from @table where name=@tj
select max(id) from(select id from @table where name='A' intersect
select id from @table where name='B') aa
else
select max(id) from @table where name=@tj改了下1楼的
DECLARE @tb TABLE (id INT,[name] VARCHAR(10))
INSERT @tb
SELECT 1,'A' UNION ALL
SELECT 1,'B' UNION ALL
SELECT 2,'A' UNION ALL
SELECT 3,'B';SELECT *
FROM ( SELECT DISTINCT
b.id ,
STUFF(( SELECT ',' + a.[name]
FROM @tb a
WHERE a.id = b.id
FOR
XML PATH('')
), 1, 1, '') AS [name]
FROM @tb b
) c
WHERE [name] = '查询条件'