0
SELECT
  *, (SELECT SUM(rating) FROM votes WHERE votes.postId = posts.id) AS rating
FROM posts
WHERE rating > 10

我的表中有多个条目,其中具有相应帖子 ID 的投票评分总和大于 10,但此查询未返回任何结果。为什么?

这是我的数据库结构的相关部分:

TABLE posts
 - id

TABLE votes
 - postId
 - rating

任何帮助将不胜感激。

4

2 回答 2

0

您需要命名子查询列,像这样

SELECT * FROM (SELECT sum(rating) as rating_sum, * FROM posts) AS rating INNER JOIN posts on posts.id = rating.id WHERE rating.rating_sum>10

于 2012-04-14T18:52:46.320 回答
0

试试这个。

 SELECT posts.*, (select SUM(rating) as totalRating 
                    from votes 
                   where votes.postid = posts.id) as totalRating
 FROM   posts 
 WHERE  totalRating > 10 
于 2012-04-14T19:31:41.917 回答