我正在使用此查询连接我的学生表和考勤表,
我的问题是,有时,出勤表没有价值
它没有返回任何价值。
<?php
if($_SERVER['REQUEST_METHOD']=="POST"){
include('include/connection.php');
showData();
}
function showData(){
global $connect;
$teacher_id = $_POST['teacher_id'];
$subject_id = $_POST['subject_id'];
$date = $_POST['date'];
$query ="
SELECT s.student_name
, s.student_number
, s.student_section
, s.subject_id
, s.fingerprint_id
, s.teacher_id
, a.status
FROM tbl_student s
LEFT
JOIN tbl_attendance a
on s.subject_id=a.subject_id
WHERE s.subject_id = '$subject_id'
and a.date='$date'
and s.teacher_id = '$teacher_id';";
$result =mysqli_query($connect,$query);
$number_of_rows = mysqli_num_rows($result);
$temp_array=array();
if($number_of_rows>0){
while($row=mysqli_fetch_assoc($result)){
$temp_array[]=$row;
}
}
header('Content-Type: application/json');
echo json_encode(array("student"=>$temp_array));
mysqli_close($connect);
}
?>
即使出勤表没有价值,我想要实现的目标是什么 我仍然可以看到学生的领域 甚至可以使用SQL查询吗?感谢
答案 0 :(得分:2)
SELECT student.student_name,student.student_number,student.student_section,student.subject_id,student.fingerprint_id,student.teacher_id,attendance.status
FROM tbl_student student
LEFT JOIN tbl_attendance attendance on student.subject_id=attendance.subject_id and attendance.date='$date'
WHERE student.subject_id='$subject_id' and student.teacher_id='$teacher_id';
尝试上面的代码。希望这会有所帮助。
由于您使用attendance.date='$date'
WHERE
子句在学生表上创建条件,因此排除了不满足此条件的记录。
因此,而不是通过ON
上的LEFT JOIN
条款将该条件置于何处。
这将实现您的目标。
答案 1 :(得分:2)
您必须将表格attendance
的字段从where
移至on
条件:
$query ="SELECT student.student_name,student.student_number,student.student_section,student.subject_id,student.fingerprint_id,student.teacher_id,attendance.status
FROM tbl_student student
LEFT JOIN tbl_attendance attendance on student.subject_id=attendance.subject_id and attendance.date='$date'
WHERE student.subject_id='$subject_id' and student.teacher_id='$teacher_id';";
因为首先执行join语句然后执行where,如果你访问表tbl_attendance
,其中所有列都为null,它们将被过滤掉。
提示:阅读准备好的语句以提供SQL注入