繁体   English   中英

MySQL选择ID匹配的另一个表的行数

[英]MySQL select count of another table's rows where the IDs match

我试图从一个表中选择另一个表的计数,其中 id 与原始 tableID 匹配,类似于:

select *, (count(0) from table2 where table2.table1ID = table1.table1ID) count
    from table1

这个的 mySQL 语法是什么?

select
  table1.*,
  if(table2.table1ID is null,0,count(*))
from
  table1
  left join table2 on table1.table1ID = table2.table1ID
group by
  table1.table1ID;
SELECT COUNT(b.*) FROM table1 as a
LEFT JOIN table2 as b ON a.tableID = b.tableID

暂无
暂无

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

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