开发者

How can i fix my query that shows me four times each of the products instead of one?

开发者 https://www.devze.com 2023-03-24 02:35 出处:网络
I am trying to export all the products and some info about them from my mySQL so I wrote the following query

I am trying to export all the products and some info about them from my mySQL so I wrote the following query

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

for a reason, it outputs every product four times. Can anyone help me to find out why?

Update:开发者_开发问答 There are only 122 products in cscart_products. The results after this query are 488.


Add GROUP BY cscart_products.product_id

Your query:

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


SELECT DISTINCT solves your problem: http://www.sql-tutorial.com/sql-distinct-sql-tutorial/


Try left join. http://phpweby.com/tutorials/mysql/32


Either a product has several descriptions or categories, or a category has several descriptions, or there are duplicate entries on your database.


Can anyone help me to find out why?

Here's a clue. The wildcard character you are using is used to display all columns/fields. There are four tables used in the query. Now we can't be certain of anything as the database schemas haven't been posted.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号