user2363025
user2363025

Reputation: 6505

join two mysql queries to order the first query by the result of the second

I initially had a web service which ran the first query (post details) and within the while loop of its results, I was running the second query to retrieve the number of comments on a post. I need to try and combine the two as now I am having to order the webservice by number of comments.

1. SELECT ReportID, Title, Description, posts.Pic, DatePosted, posts.UserID, FName, SName, users.Pic as userPic, 
                photoWidth, photoHeight
                FROM posts
                INNER JOIN Users 
                ON Users.UserID = posts.UserID 
                WHERE private = 0
                ORDER BY ReportID Desc
                LIMIT ?, 10

2. SELECT COUNT(ReportID) as numComments FROM Comments WHERE ReportID =? AND Comment IS NOT NULL

I'm unsure how to achieve this. Will I need to make a derived table?

My initial attempt:

SELECT ReportID, Title, Description, posts.Pic, DatePosted, posts.UserID, FName, SName, users.Pic as userPic, 
                photoWidth, photoHeight, numComments 
                FROM posts
                INNER JOIN Users 
                ON Users.UserID = posts.UserID 
                WHERE private = 0 AND numComments = (SELECT COUNT(ReportID) as numComments FROM Comments WHERE ReportID = ReportID AND Comment IS NOT NULL)
                ORDER BY numComments DESC

This gives the issue unknown column numComments in field list

Posts:

- ReportID (primary)
- Title
- Description
- Pic
- private
- DatePosted (epoch)
- photoWidth
- photoHeight

Comments:

- CommentID (primary)
- UserID
- ReportID (linking key)
- Comment (can be null if type = 'like')
- dateposted (epoch)
- type ('comment' or 'like')

Upvotes: 0

Views: 65

Answers (3)

syck
syck

Reputation: 3029

Suggestion: JOIN the Comments table too and GROUP BY on it.

SELECT ReportID, Title, Description, posts.Pic, DatePosted, posts.UserID, 
      FName, SName, users.Pic as userPic, photoWidth, photoHeight, 
      COUNT(CommentID) AS numComments
    FROM posts
    INNER JOIN Users ON Users.UserID = posts.UserID 
    LEFT JOIN Comments ON Comments.ReportID = posts.UserID 
    WHERE private = 0
    GROUP BY Comments.ReportID
    ORDER BY numComments DESC
    LIMIT ?, 10 

EDIT: Changed the second JOIN to a left LEFT JOIN, so reports without any comments will also be retrieved.

Upvotes: 0

alanmanderson
alanmanderson

Reputation: 8200

If I understand your question correctly I think what you want is the following:

SELECT Posts.*, count(Comments.ReportID) as CommentCount FROM Posts 
LEFT JOIN Comments
ON Comments.ReportID = Posts.ReportID
WHERE private = 0
GROUP BY Comments.ReportID
ORDER BY CommentCount, ReportID Desc;

Obviously, you will need to adjust it to contain all the fields you want and any other joins you want to do.

Here is a demo.

This will get all the posts as well as the number of Comments in each post.

Upvotes: 1

user2843259
user2843259

Reputation: 1

I don't have the data structures, but i think you could use this, using the count in a sub query

SELECT 
ReportID, Title, Description, posts.Pic, DatePosted, posts.UserID, FName, SName, users.Pic as userPic, 
                photoWidth, photoHeight, numComments.numComments 
                FROM posts
                INNER JOIN Users 
                ON Users.UserID = posts.UserID 
            WHERE private = 0 AND ReportID = (SELECT COUNT(ReportID) as numComments FROM Comments WHERE AND Comment IS NOT NULL GROUP BY ReportID) numComments
            ORDER BY numComments DESC

Upvotes: 0

Related Questions