So I want to count and echo the number of rows from table2
, which have the same id
row
as table1
.
I understand the function mysqli_num_rows
funtion, but how can I make it count the rows
from table2
that have the matching id
?
Total newbie, and still searching. Thought this might help me get in the right direction. Thanks for any help !
mysqli_num_rows return the number of rows from a result. You should write an other query to return the number of rows in table 2 with the same id
SELECT COUNT(*)
FROM table2
WHERE table2.id = <id>
CREATE TABLE table1 (id INT);
CREATE TABLE table2 (id INT);
INSERT INTO table1 VALUES (1), (2), (3), (4);
INSERT INTO table2 VALUES (2), (4);
SELECT COUNT(*)
FROM table1
JOIN table2
ON table1.id = table2.id;
Try it here: http://sqlfiddle.com/#!9/0d289/1
Try this:
SELECT `table2`.`id`, COUNT(`table2`.`id`) AS `num_rows`
FROM `table2`
LEFT JOIN `table1` ON `table2`.`id` = `table1`.`id`
WHERE `table2`.`id` IS NOT NULL
GROUP BY `table2`.`id`
ORDER BY `table2`.`id` ASC
To only count
select count(*)
from table1, table2
where table1.id = table2.id
To know what id and how much
select table2.id, count(*)
from table1, table2
where table1.id = table2.id
group by table2.id
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.