简体   繁体   中英

Count number of rows in each group in MySQLi

I know this is basic but I find it tricky and couldn't quite phrase the question right in order to find the answer.

What I'd like to do is take these tables -

Table names
name   | nameID |
-----------------
Jhon   | 1
Bob    | 2
George | 3
Kim    | 4

Table mark
nameID | child
--------------
1      | 0
1      | 0
1      | 1
2      | 1
2      | 0
2      | 1
3      | 0
3      | 0
4      | 1
4      | 1
4      | 1
4      | 0

And then count how many times each name from table names appears in table mark with child=1, so you'll get:

name   | count
--------------
Jhon   | 1
Bob    | 2
Kim    | 3

try this.

select n.name, count(*)
from names n, mark m
where n.nameID = m.nameID and m.child = '1'
group by n.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