SQL Query选择相同的数据

时间:2015-09-08 12:27:15

标签: mysql sql

在我的查询中,rating_foodrating_servicerating_decor会为我的所有结果返回相同的值。

select `restaurants`.*, `rating`.*, (rating.rating_food + rating.rating_service + rating.rating_decor) / 3 as rating_total from
(
SELECT
avg(reviews.rating_food) * (- 1 / (0.33 * count(reviews.id) + 1) + 1) + 5 * 1 / (count(reviews.id) + 1 ) as rating_food,
avg(reviews.rating_service) * (- 1 / (0.33 * count(reviews.id) + 1) + 1) + 5 * 1 / (count(reviews.id) + 1 ) as rating_service,
avg(reviews.rating_decor) * (- 1 / (0.33 * count(reviews.id) + 1) + 1) + 5 * 1 / (count(reviews.id) + 1 ) as rating_decor
FROM restaurants
JOIN reviews ON reviews.restaurant_id = restaurants.id
)
as rating, restaurants
order by `rating_total` asc limit 12

预期结果:
一个包含12个结果的数组,其中包含三个额外的计算列(rating_food等)

现在我得到了12个结果,但他们的所有rating_foodrating_servicerating_decor都返回了所有结果的相同值。

1 个答案:

答案 0 :(得分:2)

您需要将nodeName子查询与ratings表联接回来。

试试这个:

restaurants

SQL小提琴:http://sqlfiddle.com/#!9/50b39/4/0