设表有两列
id   name
1    A
1    B
2    A
3    B

如果输入A,B则返回1,输入A则返回2,输入B则返回3

解决方案 »

  1.   


    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
      

  2.   

    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楼的
      

  3.   

    换个思路……
    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] = '查询条件' 
      

  4.   

      很好的题目,2楼真强,把charindex玩转的如此犀利