i have two tables as below:
firsttable
id | cat_id | name |
---|---|---|
1 | 2 | name_01 |
2 | 2 | name_02 |
3 | 1 | name_03 |
4 | 3 | name_04 |
5 | 3 | name_04 |
secondtable
id | name |
---|---|
1 | cat_01 |
2 | cat_02 |
3 | cat_03 |
my question is how can i create below table result?
id(secondtable) | name(secondtable) | count(firsttable) |
---|---|---|
1 | cat_01 | 1 |
2 | cat_02 | 2 |
3 | cat_03 | 2 |
Advertisement
Answer
select t2.id,t2.name, (select count(*) from firsttable t1 where t1.cat_id=t2.id )as count from secendtable t2;