一个表如下
ID  TEST TEST_2  return  shoucount 
1    a     t1     A        3
2    a     t2     B        2
3    a     t3     C        1
4    b     c1     D        8查询,输入两个变量 b1 b2,查找出 TEST like '%b1%' or TEST_2 like '%b2%' 且shoucount 和ID为最小的return值showcount 和id的结果是在 TEST like '%b1%' or TEST_2 like '%b2%' 的结果
如 b1=a,b2=t,则输出B

解决方案 »

  1.   


    select [return]  
    from tb a
    where (  TEST like '%'+@b1+'%' or TEST_2 like '%'+@b2+'%' ) -- TEST like '%b1%' or TEST_2 like '%b2%' 
    and not exists (select 1 from tb b where b.TEST=a.TEST and b.TEST_2=a.TEST_2 and b.ID>a.ID) -- 且ID为最小的
    and not exists (select 1 from tb b where b.TEST=a.TEST and b.TEST_2=a.TEST_2 and b. shoucount>a.shoucount) -- 且shoucount为最小的
      

  2.   

    且shoucount 和ID为最小的如何理解?
    在你的例子中b1=a,b2=t筛选出3条记录
    ID  return  shoucount 
    1   A        3 
    2   B        2 
    3   C        1 
    "且shoucount 和ID为最小的return值"
    (shoucount最小为1,return:C; ID最小为1,return:A)
    如何得到"输出B"?
    什么逻辑?