簡體   English   中英

在MySQL查詢的WHERE子句中使用子選擇的結果

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM