简体   繁体   中英

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

I'm trying to select from one table with the count of another table where the id matches the original tableID sort of like:

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

What's the mySQL syntax for this?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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