列出所有非付费学生,但有一些问题

时间:2013-09-28 10:18:16

标签: php while-loop

我试图列出所有在两个日期范围之间不支付会费的学生。我首先从费用表中选择所有学生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 个答案:

答案 0 :(得分: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'")

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

$学生在你的while循环中被覆盖,每个新的学生ID,只剩下最后一个。让$ students成为一个阵列:

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

并改变第二个问题:

WHERE student.id NOT IN (".implode(",", $students).")

答案 3 :(得分: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');