【问题标题】:sql query cannot read from another table (mysql table realations)sql查询无法从另一个表中读取(mysql表关系)
【发布时间】:2014-05-14 10:56:17
【问题描述】:

我的数据库叫:students

MY students 表是:有列的学生:

STUDENT_ID , STUDENT NAME, ETC.

我的缺勤表是:带列的缺勤:

ABSENCE_ID, STUDENT_ID, ETC.

考虑到students_id,它应该计算每个学生有多少缺勤记录,并在表格中显示students_id,例如:

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

注意:必须从学生表中读取 STUDENT_ID,而不是从缺勤表中读取这是问题!!!!

这是我的两个问题

$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");

第一个查询运行良好(它计算表上的记录并告诉我缺勤的次数)

第二个查询不工作,我希望这个查询工作并为两者进行一个查询

我的 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>";
}

【问题讨论】:

  • 第二个查询对我来说没有意义。你希望它完成什么?它选择的两个 id 将始终相同...
  • 我放在那里的 mysql_fetch_array 仅适用于第一个查询。第二个查询与 result2 和 row2 相同,但我仅将其作为示例发布
  • 那么第二个查询的意义何在?
  • @DiellAbazi 不仅会放GROUP BY吗?像这样:SELECT students.student_id, count(*) as ABSENCES FROM students s INNER JOIN absences a ON s.student_id = a.student_id GROUP BY a.student_id
  • @LucasHenrique 当我做类似的事情时它说:Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\at\retrieve.php on line 41 我应该使用 foreach 而不是 while ???

标签: php mysql sql


【解决方案1】:

您可以使用这个单一查询来完成您的任务:

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

这将为您提供所有学生 ID 的列表以及每个学生 ID 的总缺勤情况。无需运行两个查询。如果您需要有关学生的其他数据,只需将其添加到 SELECT 下的字段列表中:s.student_names.student_age 等...

在此处查看实际操作:SQL Fiddle

还有,don't use mysql_*

【讨论】:

  • MARK 你救了我的命,如果我现在能给你 100 万的声望,我会这么做的!!!!谢谢队友
  • @DiellAbazi LOL 很高兴我能帮上忙!
【解决方案2】:

是否使用第二个查询返回每个学生的许多缺勤记录。

$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>";
}

但是,没有INNER JOIN,第一个查询的工作方式相同。仅当使用第二个查询返回一个存在于学生表中的字段时才可接受,例如 studant_name

mysql_* 函数自 PHP 5.5.0 起已弃用,不推荐用于编写新代码,因为它会在未来被删除。相反,应使用 mysqliPDO_MySQL 扩展名。

【讨论】:

  • @jxmallett 我收到一个错误:警告:mysql_fetch_assoc() 期望参数 1 是资源,在第 48 行的 C:\xampp\htdocs\at\retrieve.php 中给出的布尔值

    where line 48 是while ($row = mysql_fetch_assoc($result)) {....

  • @@Lucas Henrique 它没有在表中显示任何值我不知道问题是什么
【解决方案3】:

我认为没有任何方法可以(有效地)在一个查询中获取所有信息。

// 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());

合并两者:

$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
}

旁注:您应该真正考虑使用 mysqli 或 PDO,因为从 PHP5.5 开始,直接 mysql 已被贬值。

【讨论】:

    猜你喜欢
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 2020-11-15
    • 1970-01-01
    • 2013-12-14
    • 1970-01-01
    相关资源
    最近更新 更多