![](/img/trans.png)
[英]MySQL query with sub-select in IN()-clause gives not enough results
[英]Using the result of a sub-select in the WHERE clause in a MySQL query
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
任何帮助将不胜感激。
您需要这样命名子查询列
SELECT * FROM(选择sum(rating)为rating_sum,* FROM帖子)AS评分INNER JOIN帖子中的帖子。id= rating.id WHERE评分。rating_sum> 10
尝试这个。
SELECT posts.*, (select SUM(rating) as totalRating
from votes
where votes.postid = posts.id) as totalRating
FROM posts
WHERE totalRating > 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.