跨两个表的SQL显示重复记录

时间:2016-02-12 18:19:13

标签: sql

使用此SQL:

SELECT p.* 
FROM shop_products p, shop_product_attributes c 
WHERE c.type = 'category' AND p.sequence = c.value AND c.value = '1' 

两次显示相同的记录。

它应该显示来自shop_products where shop_products.sequence的行shop_product_attributes.product_seqshop_product_attributes.value '1'

2 个答案:

答案 0 :(得分:1)

SELECT s.* 
FROM shop_products s
where s.sequence 
in(select distinct product_seq from shop_product_attributes where type = 'category' and value=1)

答案 1 :(得分:1)

您的查询与您的上一句话不匹配,您谈的是product_seq,但加入了c.value

SELECT p.* 
FROM shop_products p JOIN shop_product_attributes c 
  ON p.sequence = c.product_seq 
WHERE c.type = 'category' 
  AND c.value = '1'