[英]Count the number of rows with the same id [PHP]
So I want to count and echo the number of rows from table2
, which have the same id
row
as table1
. 所以我想计数并回显来自table2
的行数,这些行具有与table1
相同的id
row
。
I understand the function mysqli_num_rows
funtion, but how can I make it count the rows
from table2
that have the matching id
? 据我所知,功能mysqli_num_rows
funtion,但我怎么可以把它算rows
从table2
具有匹配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. mysqli_num_rows返回结果的行数。 You should write an other query to return the number of rows in table 2 with the same id 您应该编写另一个查询以返回表2中具有相同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 在这里尝试: 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 要知道什么ID和多少
select table2.id, count(*)
from table1, table2
where table1.id = table2.id
group by table2.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.