如何查询3个表并选择1个具有组和计数的结果

时间:2018-11-30 16:27:40

标签: phpmyadmin

我有3所学校,学生和投票机构。我想从所有已注册特定主题的学校(例如计算机型号)中按student_id搜索学生。计数。结果应列出所有学校和注册学生的人数。

结果看起来像这样

-ID School_Name校长联系学校No_Registered_to_Vote

  • 1个约翰大师74578966 3个大师
  • 2南马雷亚71245456南1
  • 3北卡波73475777 0
  • 4东克里斯73636698东1
  • 5西彼得72555455 0

enter image description here

$sql = "SELECT *,COUNT(leconst_name)
FROM schools LEFT JOIN regStudent ON schools.const_name=regStudent.leconst_name
LEFT JOIN voters ON (voters.student_id=regStudent.regStudent_id) 
GROUP BY schools.school_name"; 

$result = $conn->query($sql);
// output data of each row
while($row = $result->fetch_assoc()) {
   echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['school_name'] . "</td>";
echo "<td>" . $row['Headteacher'] . "</td>";
echo "<td>" . $row['contact'] . "</td>";
echo "<td>" . $row['school'] . "</td>";
echo "<td>" . $row['COUNT(leconst_name)'] . "</td>";

0 个答案:

没有答案