id         商品id     参数id       
1           35          1
2           35          2
3           38          1
4           38          3
5           37          1
以参数id为条件查询商品id,如输入1查询35 37 38  再输入2  需要把1条件累加   查询出  35
 本人想法是 自连查询, 请问有什么效率更高的方法,因为如果查询条件过多,会导致自连N次。

解决方案 »

  1.   

    输入1查询35 37 38 
    select * from table1 where 参数id=1;输入2  需要把1条件累加   查询出  35
    select * from table1 where 参数id=2;
      

  2.   

    SELECT * from ttp a where 参数id=1 and exists(select 1 from ttp where a.商品id=商品id and 参数id=2)
      

  3.   


    我想达到的效果是
    select * from table1 as tb1
    inner join table1 as tb2
    where tb1.参数id = 1 and tb2.参数id = 2最后查出来结果是35
      

  4.   

    select *
    from table1 t
    where 参数id = 2
    and exists (select 1 from table1 where 参数id = 1)
      

  5.   


    SELECT a.* from ttp a left join ttp b on a.商品id=b.商品id 
    where b.参数id=2 and  a.参数id=12楼的代码已经可以达到目的
      

  6.   

    select *
    from table1 t
    where 参数id = 2
    and exists (select 1 from table1 where 商品id=a.商品id and 参数id = 1 )
    and exists (select 1 from table1 where 商品id=a.商品id and 参数id = 3 )
    and exists (select 1 from table1 where 商品id=a.商品id and 参数id = 4 )
      

  7.   

    用2楼的代码SELECT * from ttp a where 参数id=1 and exists(select 1 from ttp where a.商品id=商品id and 参数id=2)
    and
    exists(select 1 from ttp where a.商品id=商品id and 参数id=3)
    ....
      

  8.   

    这样的话跟自连的话应该没什么区别吧...
      SELECT * FROM table1WHERE 参数id IN( 1,2) GROUP BY 商品id HAVING COUNT(商品id)=2
       
      count(商品id)=2  2是 in里面参数的个数 
      这样也可以实现效果。 不知道有什么弊病没。
      

  9.   

    select proid from table t1 where id = 1 join (select proid from table where id = 2) t2 on(t1.proid = t2.id);给点分吧苦逼得我