繁体   English   中英

计算具有相同ID的行数[PHP]

[英]Count the number of rows with the same id [PHP]

所以我想计数并回显来自table2的行数,这些行具有与table1相同的id row

据我所知,功能mysqli_num_rows funtion,但我怎么可以把它算rowstable2具有匹配id

共有新手,仍在搜索中。 以为这可以帮助我朝正确的方向前进。 谢谢你的帮助 !

mysqli_num_rows返回结果的行数。 您应该编写另一个查询以返回表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;

在这里尝试: http : //sqlfiddle.com/#!9/0d289/1

尝试这个:

    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

只算

select count(*)
  from table1, table2
  where  table1.id = table2.id

要知道什么ID和多少

select table2.id, count(*)
  from table1, table2
    where  table1.id = table2.id
  group by table2.id 

sqlfiddle.com上的第二种情况

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM