gpt4 book ai didi

php - 计算另一行中的条目数

转载 作者:行者123 更新时间:2023-11-29 14:19:44 24 4
gpt4 key购买 nike

我正在从一个表(名称)中选择数据。在另一个表中我有老师的名字。

当我从表中选择数据时,如何计算每位教师的总姓名?

示例:

$q = mysql_query("SELECT * FROM names ORDER BY limit 50");
while($row = mysql_fetch_array($q)){
$name = $row['name'];
$teacher $row['teacher'];
$total = ?????

echo "the name is $name and the teacher is $teacher (total number for this teacher is $total";

}

我怎样才能做到这一点?

最佳答案

SELECT teachersTable.*, COUNT(`names`.`id`) as NamesCount FROM `teachersTable`
LEFT JOIN `names` ON `teachersTable`.`id`=`names`.`teacherid`
GROUP BY `teachersTable`.`id`

使用它作为查询,NamesCount 列应该是每个教师的姓名数量

关于php - 计算另一行中的条目数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11946045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com