-1

我试图列出所有在两个日期范围内不缴纳会费的学生。我首先从费用表中选择所有学生 id,然后将其存储在一个变量中,然后将这些 id 与学生表进行比较以列出所有非付费学生。但我的问题是它只适用于最后一个 id。假设我得到学生 id、1 和 2,那么查询仅适用于学生 id 2。这是代码。

$query = mysql_query("SELECT student_id,created FROM fee WHERE DATE(created) between  '$sqldate1' AND '$sqldate2'");
while($rows = mysql_fetch_array($query)) {

  $students = $rows['student_id'];
  //echo $students;
  $link = mysql_query("SELECT fee.class_id,fee.section_id,fee.student_id,fee.section_group,
         fee.student_fee,fee.test_fee,fee.other_charges,fee.created,fee.fee_month,
class.class_id,class.class_name,section.section_id,section.section_name,student.id,
student.student_name FROM fee LEFT JOIN class ON(fee.class_id=class.class_id)
 LEFT JOIN section ON(fee.section_id=section.section_id) 
 LEFT JOIN student ON(fee.student_id=student.id) WHERE student.id !='$students'")
  or die(mysql_error());

  $num = mysql_num_rows($link);
}
4

4 回答 4

1

您必须在连接条件上指定表名,如下所述

 $link = mysql_query("SELECT fee.class_id,fee.section_id,fee.student_id,fee.section_group,
             fee.student_fee,fee.test_fee,fee.other_charges,fee.created,fee.fee_month,
    class.class_id,class.class_name,section.section_id,section.section_name,student.id,
    student.student_name FROM fee LEFT JOIN class ON(fee.class_id=class.class_id)
     LEFT JOIN section ON(fee.section_id=section.section_id) 
     LEFT JOIN student ON(fee.student_id=student.id) WHERE DATE(fee.created) between  '$sqldate1' AND '$sqldate2'")
于 2013-09-28T10:28:18.707 回答
0

你只能有一个查询:

SELECT fee.class_id,fee.section_id,fee.student_id,fee.section_group,
             fee.student_fee,fee.test_fee,fee.other_charges,fee.created,fee.fee_month,
    class.class_id,class.class_name,section.section_id,section.section_name,student.id,
    student.student_name FROM fee LEFT JOIN class ON(fee.class_id=class.class_id)
     LEFT JOIN section ON(fee.section_id=section.section_id) 
     LEFT JOIN student ON(fee.student_id=student.id) WHERE student.id NOT EXISTS (SELECT student_id,created FROM fee WHERE DATE(created) between  '$sqldate1' AND '$sqldate2');
于 2013-09-28T10:36:33.897 回答
0

您正在迭代初始查询中的每个 student_id,并每次使用不同的单个学生运行您的第二个查询。由于您在每次迭代时都覆盖了 $students、$link 和 $num 而没有将它们存储在某个地方,因此这些数据会丢失,并且您将得到循环的最后一次迭代发生的任何内容。

您需要首先构建一个您希望检查的所有 student_id 的列表,然后在您的第二个查询中使用它进行比较(这需要在 while 循环之外)。

于 2013-09-28T10:25:54.320 回答
0

$students 在你的 while 循环中被每个新的学生 id 覆盖,只留下最后一个。使 $students 成为一个数组:

$students[] = $row['student_id'];

并更改第二个查询:

WHERE student.id NOT IN (".implode(",", $students).")
于 2013-09-28T10:27:55.030 回答