SELECT
*, (SELECT SUM(rating) FROM votes WHERE votes.postId = posts.id) AS rating
FROM posts
WHERE rating > 10
There are multiple entries in my table where the sum of the ratings in votes with the corresponding post ID is greater than 10, but this query is not returning any results. Why?
Here is the relevant portion of my database structure:
TABLE posts
- id
TABLE votes
- postId
- rating
Any help would be greatly appreciated.
You need to name the subquery column, like this
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