[英]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. 最初,我有一个运行第一个查询(帖子详细信息)的Web服务,并且在其结果的while循环内,我正在运行第二个查询以检索帖子中的评论数。 I need to try and combine the two as now I am having to order the webservice by number of comments.
我需要尝试将两者结合起来,因为现在我必须按评论数订购Web服务。
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 这使字段列表中的问题未知列numComments
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')
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. 显然,您需要对其进行调整,以包含所需的所有字段以及您想要执行的任何其他联接。
This will get all the posts as well as the number of Comments in each post. 这将获取所有帖子以及每个帖子中的评论数。
I don't have the data structures, but i think you could use this, using the count in a sub query 我没有数据结构,但我认为您可以在子查询中使用count来使用它
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
Suggestion: JOIN
the Comments table too and GROUP BY
on it. 建议:也
JOIN
评论表并在其上使用GROUP BY
。
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. 编辑:将第二个JOIN更改为左LEFT JOIN,因此也将检索没有任何注释的报告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.