我如何修复我的查询,显示每个产品而不是一个产品四次?

时间:2011-07-29 21:40:58

标签: php mysql sql

我正在尝试从mySQL导出所有产品和一些有关它们的信息,所以我写了以下查询

SELECT * FROM products
JOIN descriptions
ON products.product_id = descriptions.product_id
JOIN category_descriptions
ON categories.category_id = descriptions.category_id

有一个原因,它输出每个产品四次。任何人都可以帮我找出原因吗?

更新: cscart_products中只有122个产品。此查询后的结果为488。

5 个答案:

答案 0 :(得分:1)

添加GROUP BY cscart_products.product_id

您的查询:

SELECT * FROM cscart_products
JOIN cscart_product_descriptions
ON cscart_products.product_id = cscart_product_descriptions.product_id
JOIN cscart_products_categories
ON cscart_products.product_id = cscart_products_categories.product_id
JOIN cscart_category_descriptions
ON cscart_products_categories.category_id = cscart_category_descriptions.category_id
GROUP BY cscart_products.product_id

答案 1 :(得分:0)

SELECT DISTINCT解决了您的问题:http://www.sql-tutorial.com/sql-distinct-sql-tutorial/

答案 2 :(得分:0)

答案 3 :(得分:0)

产品有多个描述或类别,或者类别有多个描述,或者数据库中有重复的条目。

答案 4 :(得分:0)

  

任何人都可以帮我找出原因吗?

这是一个线索。您使用的通配符用于显示所有列/字段。查询中使用了四个表。现在我们无法确定任何事情,因为数据库模式尚未发布。