dimanche 11 octobre 2015

How to count occurrences of a foreign table id in a certain table in SQLite?

So I have a table that looks like this :

Table A
a_id    b_id

Table B
b_id

I want to count hoe many times the id in table B is shown or used as a value in the b_id column in Table A. Presumably, the result should be something like this :

ID     count
1      10
2      8
3      11

I considered using the query

Select b_id from table_b 

to get all the ids. Then iterate over each one of them and count them like this :

Select count(*) from table_a where b_id = ''

But the process is just too long. I want to make it a little shorter in one query. But I don't know how to iterate over rows in SQLite language. Even just a push to the right direction will help.

Aucun commentaire:

Enregistrer un commentaire