表A,产品表,主键PID
表B,参数表,pid=a.pid, value(参数值)现在我表a中有三个产品,1、2、3这三个产品对应的参数表分别都有很多参数
基本结构如下表A        表B产品ID     pid    value
1          1      a
           1      b
           1      c
2          2      a
           2      e
           2      d
3          3      a
           3      e
           3      b我现在想查询输出   哪个产品的参数value=a and value=b  也就是结果能查到产品,1 和 3怎么写SQL语句啊,

解决方案 »

  1.   

    select a.id from (select pid,value from b left join a on a.id=b.pid) where b.value='a' and b.value='b'
      

  2.   

    楼上的,我试了不行,报错。我用的是MYSQL数据库
      

  3.   


      select a.id from a where b.value in (select value from b left join a on a.id=b.pid)  b.value='a' and b.value='b'
      

  4.   

    错了,select a.id from a where b.value in (select value from b left join a on a.id=b.pid)  b.value='a' or b.value='b';
      

  5.   


    select a.pid from a
    where pid in (select pid from b where b.pid=a.pid and value='a' ) and
          pid in(select pid from b where b.pid=a.pid and value='c' )
    sql 不是最好的,但是可以实现你的功能
      

  6.   

    select pid from b where b.pid in ('a' ,'b')
      

  7.   


    而且还会出现10个以上的
    pid in(select pidfrom bwhere b.pid=a.pidand value='c' )
    这样的条件,用IN太多的话,会不会太慢了?
      

  8.   

    select * from B ta left join B tb on ta.pid=tb.pid where at.value='a' and tb.value='b';
    如果只是想查PID的话用表B就可以了。如果想查产品的具体的话就要另算了。
      

  9.   

    SELECT distinct(A.Pid) FROM a A 
    INNER JOIN b B On A.pid = B.pid
    WHERE B.value='a' AND B.value='c'B.value 上要有索引 提高速度
    create index IDX_VALUE on b( value)