表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语句啊,
表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语句啊,
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'
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 不是最好的,但是可以实现你的功能
而且还会出现10个以上的
pid in(select pidfrom bwhere b.pid=a.pidand value='c' )
这样的条件,用IN太多的话,会不会太慢了?
如果只是想查PID的话用表B就可以了。如果想查产品的具体的话就要另算了。
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)