如何在sql中连接具有多个条件的表?

时间:2015-03-25 15:28:50

标签: sql

我在这里显示两个表:

enter image description here

我想只在所有product.model都在table pc中时选择product.maker。 所以,对于同一个制造商,下一个条件必须是真的:

product.model = pc.model

1 个答案:

答案 0 :(得分:0)

  

"我想只在他的所有product.model都在的时候选择product.maker   table pc。"

我将此解释为您想要一个制造商列表,以便分配给制造商的每个模型都在pc表中。这比简单连接更微妙。它更像是left join,具有聚合和适当的条件:

select p.maker
from product p left join
     pc
     on p.model = pc.model
group by p.maker
having count(*) = count(pc.model);

having条件是验证product中每个模型是否匹配的一种方法。