简体   繁体   中英

MySQL join not working on subquery

Here's my database:

在此处输入图片说明

This query select all supplements:

SELECT a.id, a.name, a.image, a.url_segment, COUNT(b.id) AS reviews_count, ROUND(AVG(b.rating), 2) AS reviews_rating, (((SELECT COUNT(*) FROM reviews) * (SELECT AVG(rating) FROM reviews)) + (COUNT(b.id) * AVG(b.rating))) / ((SELECT COUNT(*) FROM reviews) + COUNT(b.id)) AS bayesian_rating
FROM (`supplements` AS a)
LEFT JOIN `reviews` AS b ON `b`.`supplements_id` = `a`.`id`
GROUP BY `a`.`id`
ORDER BY `bayesian_rating` DESC

And this, all supplements from one subcategory (in this case, with id = 1):

SELECT a.id, a.name, a.image, a.url_segment, COUNT(b.id) AS reviews_count, ROUND(AVG(b.rating), 2) AS reviews_rating, (SELECT text FROM reviews WHERE supplements_id = a.id ORDER BY id DESC LIMIT 1) AS reviews_latest_text, (((SELECT COUNT(*) FROM reviews LEFT JOIN supplements ON (supplements.id = reviews.supplements_id AND supplements.subcategories_id = 1)) * (SELECT AVG(rating) FROM reviews LEFT JOIN supplements ON (supplements.id = reviews.supplements_id AND supplements.subcategories_id = 1))) + (COUNT(b.id) * AVG(b.rating))) / ((SELECT COUNT(*) FROM reviews LEFT JOIN supplements ON (supplements.id = reviews.supplements_id AND supplements.subcategories_id = 1)) + COUNT(b.id)) AS bayesian_rating
FROM (`supplements` AS a)
LEFT JOIN `reviews` AS b ON `b`.`supplements_id` = `a`.`id`
WHERE `a`.`subcategories_id` =  '1'
GROUP BY `a`.`id`
ORDER BY `bayesian_rating` DESC

The bayesian rating of the same supplements should be different on each query, but on both it's returning the same.

Here's is the part where I calculate the bayesian rating on the first query:

(((SELECT COUNT(*) FROM reviews) * (SELECT AVG(rating) FROM reviews)) + (COUNT(b.id) * AVG(b.rating))) / ((SELECT COUNT(*) FROM reviews) + COUNT(b.id)) AS bayesian_rating

On the second:

(((SELECT COUNT(*) FROM reviews LEFT JOIN supplements ON (supplements.id = reviews.supplements_id AND supplements.subcategories_id = 1)) * (SELECT AVG(rating) FROM reviews LEFT JOIN supplements ON (supplements.id = reviews.supplements_id AND supplements.subcategories_id = 1))) + (COUNT(b.id) * AVG(b.rating))) / ((SELECT COUNT(*) FROM reviews LEFT JOIN supplements ON (supplements.id = reviews.supplements_id AND supplements.subcategories_id = 1)) + COUNT(b.id)) AS bayesian_rating

For some reason the join is not making any difference to the result.

Since reviews are children of supplements, joining to supplements won't return anything different for reviews.

I think it's expected that the two queries return the same.


Also, by mathematical definition of AVG , the term

(SELECT COUNT(*) FROM reviews) * (SELECT AVG(rating) FROM reviews)

is identical to

(SELECT SUM(rating) FROM reviews)

so you can simplify (and speed up) your query but substituting this in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM