如何计算mysql中的行数?

9q78igpj  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(700)
<?php
        $query="select usertable.id, name, username, dateofbirth, user_id from usertable INNER JOIN passbook on passbook.user_id=usertable.id ";
        $result= mysqli_query($sql, $query);
        $rowcount=mysqli_num_rows($result);
        printf("Result set has %d rows.\n",$rowcount);
        while($row=mysqli_fetch_array($result))
        {
            echo "<tr>";
            echo "<td>";
            echo $row["id"];
            echo "</td>";
            echo "<td>";
            echo $row["name"];
            echo "</td>";
            echo "<td>";
            echo $row["dateofbirth"];
            echo "</td>";               
            echo "<td>";
            echo $row["user_id"];
            echo "</td>";

            echo "</tr>";
        }

    ?>

我试图在表中显示某个特定用户的图书数量。我使用了mysqli\u num\u rows查询,但它返回视图中的总行数。我想以表格数据的形式显示每个用户的图书数量。请帮忙。

esyap4oy

esyap4oy1#

从passbook.user\u id=usertable.id上的usertable inner join passbook中选择count(*)作为count、usertable.id、name、username、dateofbirth、user\u id

bfhwhh0e

bfhwhh0e2#

在sql语句中使用groupby进行聚合(这不是要解决的php问题,而是db解决方案):

select count(*) as bookstaken, usertable.id, name, username, dateofbirth, user_id from usertable INNER JOIN passbook on passbook.user_id=usertable.id GROUP BY usertable.id, name, username, dateofbirth, user_id`

相关问题