简体   繁体   中英

How to use join to select like count of returned message rows

Context

I'm having trouble understanding the application of the JOIN statement when I try to return rows of message values and find their corresponding like counts, which are stored in a different table. The first table I'm trying to pull from is one that contains the messages I want:

SELECT forums.forum_id, forums.message FROM forums LIMIT 3

I'm then trying to use the resulting forum_id values to insert into the second query, which obtains the like counts:

SELECT COUNT(forumvotes.forum_id) AS voteCount FROM forumvotes JOIN forums ON forumvotes.forum_id = forums.forum_id 
WHERE forumvotes.forum_id = [insert returned forum ids here]

Question

I believe that I can combine these two queries into one by using a join, but I'm unsure of how to do it. When I combine the two like so, the new voteCount column returns the count of all the forum votes that have been inserted, not just the count of the likes of the three specific messages I want:

SELECT forums.forum_id, forums.message, 
(SELECT COUNT(forumvotes.forum_id) AS voteCount FROM forumvotes JOIN forums ON forumvotes.forum_id = forums.forum_id WHERE forumvotes.forum_id = forums.forum_id) AS voteCount 
FROM forums LIMIT 3

So, the query returns three rows, each of which contains the invididual messages (which is good), but they also return the same voteCount value of 13 because there have been 13 likes inserted into the database, but each row's voteCount value should only be one or two because each message only has one or two likes.

How can I combine these two queries into one? Let me know if there's any confusion.

You need to properly correlate the subquery with the outer query. The problem is that you are using the same alias for the outer and inner, so the condition in the WHERE clause does not actually filter anything. You should be using table aliases to avoid this problem.

Is is unnecessary to use the forum table in the subquery anyway. You can write this as:

SELECT 
    f.forum_id, 
    f.message, 
    (
        SELECT COUNT(*) 
        FROM forumvotes fv 
        WHERE fv.forum_id = f.forum_id
    ) AS voteCount 
FROM forums f
ORDER BY ??
LIMIT 3

You would also need an ORDER BY clause if you want results that are consistent over consecutive executions.

From how you describe this, you would be looking to do an "IN" sub-query here.

SELECT COUNT(forumvotes.forum_id) AS voteCount FROM forumvotes 
WHERE forumvotes.forum_id IN (SELECT forums.forum_id, forums.message FROM forums LIMIT 3)

Specifically what this will be doing is returning only the ids of the first three forum rows, and joining them with some forum information

I JOIN the forums table to a nested query that holds the COUNT, GROUP BY each forum_id

SELECT f.forum_id, f.message, a.voteCount
  FROM forums f
  JOIN (SELECT COUNT(forumvotes.forum_id) AS voteCount, forumvotes.forum_id
          FROM forumvotes
         GROUP BY forumvotes.forum_id) a ON f.forum_id = a.forum_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM