简体   繁体   中英

aggregate row counts using foreign key entries

I am trying to query a table and grab ALL data in one column lets say the column we are using is id where their name is NOT NULL

SELECT id FROM AUTHORS WHERE name IS NOT NULL;

Then using the ALL rows from this query

I'd want to search another table called BOOKS using all id from the AUTHORS table, where the id matches the record.

Then the amount returned from each query where the id matches get that row count and return that instead.

Then finally aggregating the query into a result, where the id has a row count.

So that you end up with something like :

ID | Number of Entries
0  | 12
1  | 5001
2  | 1337

But I am not sure if this is possible?

you could use a join , count and group by

  SELECT a.id , a.name,  count(*)
  FROM AUTHORS a 
  INNER JOIN BOOKS b ON a.ID = B.Auhors_id AND a.name IS NOT NULL
  GROUP BY a.id , a.name 

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