How to correlate 2 tables to calculate the quantity?

Table 1: classification Table, id,name
Table 2: data Table, id,name,diqu,class_id

I want to show all the result sets classified in "Table 1", and can associate "Table 2" to get the statistical quantity of all the data under the relevant categories. I need the diqu field of where like Table 2
how to write it?
has been writing for a long time and there is an error.
or the quantity can be counted, but the result set of Table 1 is incomplete

.

the simple way is to read all the result sets of "Table 1" first, and then cycle to "Table 2" for statistics. This can be achieved, but the efficiency can be imagined.

Jun.24,2021

grouping query statistics: class table: id,name data table: id,class_id,content

SELECT A. data count (b.class_id) FROM class as a LEFT JOIN data as b ON a.id = b.class_id GROUP BY b.class_id


try this

SELECT a.id,a.name,COUNT(b.id) b_count FROM 1 a
JOIN 2 b ON a.id = b.class_id
GROUP BY a.id
HAVING b.diqu LIKE '';

my plan is:
1. First index the required class_id and statistics from Table 2
select class_id,count (1)
from Table 2
where diqu like 'keyword'

2. Then intersect with table a to get name
select id,name
from Table 1
where id in class_id

3. Assembling data

Menu