简体   繁体   English

sql查询无法从另一个表读取(mysql表实现)

[英]sql query cannot read from another table (mysql table realations)

My database is called: students 我的数据库叫做:学生

MY students table is: students with columns: 我的学生表是:有列的学生:

STUDENT_ID , STUDENT NAME, ETC.

My absences table is: absences with columns: 我的缺勤表是:列缺勤:

ABSENCE_ID, STUDENT_ID, ETC.

it should count how many absence records per student i have taking in consideration the students_id and to show the students_id in the table for example: 它应该计算每个学生有多少缺勤记录,我考虑了students_id并在表格中显示了students_id ,例如:

+------------+-----------+
| STUDENT ID | ABSENCES  |
+------------+-----------+
| 1          | 3         |
| 2          | 8         |
| 3          | 437       |
+------------+-----------+

NOTE: THE STUDENT_ID MUST BE READ FROM STUDENTS TABLE NOT FROM ABSENCES TABLE THIS IS THE PROBLEM !!!! 注意:必须从学生表而不是缺席表中读取STUDENT_ID,这是问题 !!!!

THESE ARE MY TWO QUERIES 这些是我的两个查询

$result = mysql_query("SELECT student_id, COUNT(*) AS count FROM absences GROUP BY student_id ORDER BY count DESC;");
$result2 = mysql_query("SELECT students.student_id, absences.student_id FROM students INNER JOIN absences ON students.student_id = absences.student_id");

The first query is working fine (It counts the records on table and tells me how many absences are) 第一个查询工作正常(它对表上的记录进行计数并告诉我有多少个缺勤)

The second query is not working, i want this query to work and to make ONE QUERY for both 第二个查询不起作用,我希望该查询起作用并为两个查询都做一个查询

My php code looks like this: 我的php代码如下所示:

while($row = mysql_fetch_array($result))
{
    echo "<tr>";
    echo "<td><font size=\"4\" color=\"white\">" . $row['student_id'] . "</font></td>";
    echo "<td><font size=\"4\" color=\"white\">" . $row['count'] . "</font></td>";
    echo "</tr>";
}

You can use this single query to accomplish your task: 您可以使用以下单个查询来完成您的任务:

SELECT 
    s.student_id,
    COUNT(a.student_id) as count
FROM students s
LEFT JOIN absences a ON a.student_id = s.student_id
GROUP BY a.student_id
ORDER BY count DESC

This will give you a list of all student IDs and the total absences for each. 这将为您提供所有学生证的列表以及每个学生证的缺勤总数。 No need to run two queries. 无需运行两个查询。 If you need additional data about the student, just add it to the list of fields under SELECT: s.student_name , s.student_age , etc... 如果您需要有关该学生的其他数据,只需将其添加到SELECT下的字段列表中: s.student_names.student_age等。

See it in action here: SQL Fiddle 在此处查看其运行情况: SQL Fiddle

And, ya, don't use mysql_* 而且, 不要使用mysql_ *

Whether to return many absence records per student using the second query. 是否使用第二个查询为每个学生返回许多缺勤记录。

$result2 = mysql_query("SELECT students.student_id, count(absences.student_id) as absences FROM students INNER JOIN absences ON students.student_id = absences.student_id GROUP BY absences.student_id");

while($row = mysql_fetch_array($result2))
{
    echo "<tr>";
    echo "<td><font size=\"4\" color=\"white\">" . $row['student_id'] . "</font></td>";
    echo "<td><font size=\"4\" color=\"white\">" . $row['absences'] . "</font></td>";
    echo "</tr>";
}

However, the first query works the same way without INNER JOIN . 但是,第一个查询在不使用INNER JOIN情况下的工作方式相同。 Would only be acceptable if using the second query returns a field that exists in students table, such as studant_name , for example. 仅当使用第二个查询返回学生表中存在的字段(例如studant_namestudant_name接受。

mysql_* functions is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. 从PHP 5.5.0开始不推荐使用mysql_ *函数,不建议编写新代码,因为将来会删除它。 Instead, either the mysqli or PDO_MySQL extension should be used. 相反,应使用mysqliPDO_MySQL扩展名。

I don't think there is any way to (efficiently) get all the information in one query. 我认为没有任何方法可以(有效地)在一个查询中获取所有信息。

// This will get student IDs and their total number of absences
$result = mysql_query("SELECT student_id, COUNT(absence_id) AS total_absences
    FROM absences
    GROUP BY student_id
    ORDER BY count DESC;") or die(mysql_error());

//This will get the details of each student and each absence.
//Add which ever fields you want.
$result2 = mysql_query("SELECT students.student_id, absences.absence_id
    FROM students, absences
    WHERE students.student_id = absences.student_id") or die(mysql_error());

To merge the two: 合并两个:

$totalAbsences = array();
while ($row = mysql_fetch_assoc($result)) {
    $totalAbsences[$row['student_id']] = $row['total_absences'];
}

while ($row = mysql_fetch_assoc($result2)) {
    $totalAbsencesForThisStudent = $totalAbsences[$row['student_id']];
    //Make your table here
}

Side note: You should really look into using mysqli or PDO as straight mysql is depreciated as of PHP5.5. 旁注:您真的应该研究使用mysqli或PDO,因为从PHP5.5起,纯正的mysql已弃用。

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

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