这是部门表
department_id | department_name
1 computers
2 maths
3 physics
4 mba
这是讲师表
instructor_id | department_id | name
1 1 abc
2 2 manu
3 2 raju
4 3 jaya
5 4 man
从上面的代码中如何显示每个部门的教师总数?
查询是什么? 请帮忙。
我是在codeigniter中做的。
答案 0 :(得分:1)
Peter Parker Picked A
Pack Of Pepper
答案 1 :(得分:1)
SELECT COUNT(i.instructor_id) as `instructor_count`, d.department_name
FROM instructor AS i
JOIN department ON d.department_id = i.department_id
GROUP BY i.department_id"
答案 2 :(得分:1)
您可以尝试从我的解决方案中进行概念化,看看它是否有帮助,
为避免并发症,您可以直接进行
<?php
//connection
$link = mysqli_connect("localhost","root","","database_name") or die("Couldn't make connection.");
$computer_department_id = //the value
$query = mysqli_query($link, "SELECT department_id FROM instructure_table WHERE department_id='$computer_department_id')");
$total = mysqli_num_rows($query);
echo $total; //this one will display the total number of instructors in computer department
?>
或者如果你喜欢
<?php
//connection
$link = mysqli_connect("localhost","root","","database_name") or die("Couldn't make connection.");
$computer_department = //the value
$query = mysqli_query($link, "SELECT department_id
FROM instructure_table AS t
WHERE EXISTS (SELECT department_id
FROM department_table AS d
WHERE d.department_id = t.department_id AND department_id='$computer_department')");
$total = mysqli_num_rows($query);
echo $total; //this one will display the total number of instructors in computer department
?>
因此,如果您将查询放在部门行
中department_id | department_name
1 computers //the query
2 maths //the query
3 physics //the query
4 mba //the query
即使部门的查询位于重复区域,它仍然适用于您。问候