简体   繁体   English

MySQL联接无法在子查询上运行

[英]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): 并且,这是一个子类别的所有补充(在这种情况下,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 同样,根据AVG的数学定义,

(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. 因此您可以简化(并加快)查询,但可以将其替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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