Question

I'm having an issue with mysql when doing a JOIN of two tables and filtering using HAVING for these two tables.

Assume my tables look like this:

Products:

id | Name
1  | Product 1
2  | Product 2
3  | Product 3

Reviews:

Name     | Product_id | score
Review 1 | 1          | 10000

My best guess was the following, but it doesn't work:

SELECT "products".*, sum("reviews".score) FROM "products" INNER JOIN "reviews" ON "reviews"."empire_id" = "products"."id" GROUP BY products.id HAVING sum("reviews".score)=0;

And I would like to get the products that have a score of 0, meaning that they have not been reviewed. I feel like this should be easy, but I can't seem to figure out.

Was it helpful?

Solution

It sounds like you want products that show up in the "products" table but not the "reviews" table. Perhaps take a look at SQL - find records from one table which don't exist in another?

Alternatively, replacing your INNER JOIN with a LEFT JOIN might work.

OTHER TIPS

SELECT products.id, products.name, sum("reviews".score)
FROM products
LEFT JOIN reviews ON reviews.product_id = products.id
GROUP BY products.id, products.name
HAVING sum(reviews.score)=0;

"reviews"."empire_id" = "products"."id"

Should be "reviews"."product_id" = "products"."id"

Actually your group by should have all your field name from product

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top