简体   繁体   English

加入两个mysql查询,以第二个查询的结果对第一个查询进行排序

[英]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. 显然,您需要对其进行调整,以包含所需的所有字段以及您想要执行的任何其他联接。

Here is a demo . 这是一个演示

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.

相关问题 MySQL-连接两个查询,第二个查询被第一个查询的结果过滤 - MySQL - join two queries the second being filtered by the result from the first Laravel中的MySQL子查询,如何联接查询并将第二查询的结果作为新列添加到第一查询? - MySQL sub query in Laravel, how to join queries and add second query's result as a new column to first query? 加入 2 个 mysql 查询,其中第二个查询依赖于第一个的 output - Join 2 mysql queries, where the second query depends on the output of the first 两个MySQL查询合二为一,第二个查询基于第一个查询 - Two MySQL queries in one, with the second query based on first query 在MySQL中联接两个查询以将一个查询的结果映射到另一个 - Join two queries in MySQL to map result of one query with another MYSQL - 如何连接两个查询以省略在第二个查询中找到的元素(或者可能有更好的解决方案?) - MYSQL - How to join two queries to omit elements found in the second query (or perhaps any better solution?) MySQL查询联接两个选择查询 - MySQL Query to join two select queries 我想使用 MySQL 中第二个查询中第一个查询的列将两个查询合二为一 - I want to use two queries into one using a column from first query in the second one in MySQL 如何使用第二个第一个查询的结果启动两个sql查询? - How to launch two sql queries using result of first one in second? mysql从两个表中连接查询结果 - mysql Join Query for Result from two table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM