简体   繁体   English

如何从一个表中返回一行,其中包含MySQL中第二个表中匹配id的数量?

[英]How do I return a row from one table with the number of matching id's from a second table in MySQL?

I have two tables, one of discussions and one of responses. 我有两个表,一个讨论和一个答复。 I'm wondering how to pull back each discussion row along with the number of rows from the responses table with the matching discussion ID. 我想知道如何使用匹配的讨论ID来回退每个讨论行以及响应表中的行数。

table discussions 表格discussions

  • id
  • creator
  • date
  • title

table responses 表格responses

  • id
  • creator
  • date
  • discussion_id = this is id from discussions discussion_id =这是iddiscussions

So I'd love to be able to get each row of discussions along with a COUNT() of all the responses rows that have its discussion_id 因此,我希望能够获得每一行讨论以及所有具有其discussion_idresponses行的COUNT()

This is about as far as I got: 这是我所得到的:

SELECT
    d.id,
    d.creator,
    d.date,
    d.title,
    (select count(select * from r where r.discussion_id = d.id) from r) num
FROM 
    discussions d, responses r

Am I on the right track here? 我在这里走在正确的轨道上吗?

Thanks for your time! 谢谢你的时间!

SELECT d.id, d.creator, d.date, d.title, COUNT(r.id) AS num 
FROM discussions AS d 
  INNER JOIN responses AS r ON r.discussion_id = d.id
GROUP BY d.id

As pointed out by Crwydryn you could also use a LEFT JOIN instead of the INNER JOIN . 正如Crwydryn所指出的,您也可以使用LEFT JOIN而不是INNER JOIN The difference would be that you also count the discussions with no resonses with the LEFT JOIN - the num column will then be 0 . 不同之处在于您还可以在没有与LEFT JOIN共振的情况下计算讨论 - num列将为0

You are close. 你很亲密 Try: 尝试:

SELECT
    d.id,
    d.creator,
    d.date,
    d.title,
    (select count(*) from r where r.discussion_id = d.id) num
FROM 
    discussions d

Try like this...simply use group by....it will give what u want 尝试这样......只需使用分组......它会给你想要的东西

 SELECT
        d.id,
        d.creator,
        d.date,
        d.title,
       (Select Count(*) from response where response.id= d.id) as num
    FROM 
        discussions d

Please check the following Query 请检查以下查询

SELECT
    d.id,
    d.creator,
    d.date,
    d.title,
   count(r.id)  NUM
FROM 
dicussion d join response r on d.id = r.discussion_id  
GROUP BY d.id 

I would suggest using a LEFT JOIN. 我建议使用LEFT JOIN。 A standard join will return only Discussions that have a related Response entry, so if the response count is 0 (ie no responses for that discussion), a query with a standard join won't return that discussion record. 标准连接将仅返回具有相关响应条目的讨论,因此如果响应计数为0(即,该讨论没有响应),则具有标准连接的查询将不返回该讨论记录。 Also, this is more performant than a subquery. 此外,这比子查询更高效。

select d.id,
    d.creator,
    d.date,
    d.title,
    count(r.id) as num
FROM discussions d 
    LEFT JOIN responses r on d.id = r.discussion_id
GROUP BY d.id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL:使用一个查询从第二个表返回行计数 - MySQL: Return row count from second table using one query MySQL:如何根据一个表中另一个表的ID从一个表中获取一条信息 - MySQL: How do I get a piece of information from one table based on it's id in another table 我如何总是从一个表中提取数据,又如何从第二个表中提取数据(如果MySQL中存在的话)? - How do I always pull data from one table, but also pull data from a second table if it's there in MySQL? 如何从MySQL表中选择一行? - How do I select one row from a MySQL table? PHP:从一个 mysql 表中获取最后一行的 id 号,并使用它来更新另一个表的第一行中的字段 - PHP: Taking the last row's id number from one mysql table and using it to update a field in the first row of another table MYSQL:如何将整行从一个表复制到 mysql 中的另一个表,第二个表有一个额外的列? - MYSQL: How to copy an entire row from one table to another in mysql with the second table having one extra column? 如果第二个表没有匹配信息,mysql如何从一个表中选择 - mysql how to select from one table if the second table has no matching information 如果他们的id和用户名没有出现在第二个表中,MySQL会从一个表中选择记录 - MySQL select records from one table if their id AND username do not appear in a second table 如何使用MySQL从一个表行中获取数据,并将其复制到具有相同项目编号的其他行 - How do I use MySQL to take data from one table row, and copy it to the other rows with the same item number MySQL:如何返回表中的所有行,并从另一个表中计算具有匹配ID的行数 - MySQL: How to return all rows in a table and count the amount of rows with matching ID from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM