我正在嘗試進行查詢,但它沒有顯示任何結果。我的表是:我無法查詢
TABLE teacher_student_conn:
id, teacher_id, student_id, created_at
TABLE users:
user_id, username, password, school_id, class, division, role_id, deactivated_at
TABLE schools:
school_id, school_name
我想表明我的只有這些學生是在老師的學校,他不教他們 - 我把老師從$this->uri->segment(3)
ADN學校 - 從$this->uri->segment(4)
。它必須顯示這些學生,因爲管理員可以將它們添加到教師。 我不知道如何讓我的查詢向我展示這些在老師的學校裏的學生,他不教他們。 我對查詢嘗試:
public function select_students() {
$this->db->select('users.user_id, users.username, users.school_id, users.class, users.division, users.role_id, schools.school_name, schools.region,class_divisions.division');
$this->db->from('users');
$this->db->join('teacher_student_conn','teacher_student_conn.student_id=users.user_id','left');
$this->db->join('teacher_student_conn AS T','T.teacher_id=users.user_id','left');
$this->db->join('class_divisions','class_divisions.id=users.division','left');
$this->db->join('schools','schools.school_id=users.school_id','left');
$this->db->where('(users.deactivated_at = "0000-00-00 00:00:00" OR users.deactivated_at IS NULL) AND users.role_id = 1 ');
$this->db->where('users.school_id', $this->uri->segment(4));
$this->db->where('teacher_student_conn.teacher_id', $this->uri->segment(3));
$this->db->where("teacher_student_conn.student_id IS NULL");
$result=$this->db->get();
return $result->result();
}
後呼應$這個 - > DB-> last_query();它顯示我:
SELECT users.user_id, users.username, users.school_id, users.class, users.division, users.role_id, schools.school_name, schools.region,class_divisions.division FROM users LEFT JOIN teacher_student_conn ON teacher_student_conn.student_id=users.user_id LEFT JOIN teacher_student_conn as T ON T.teacher_id=users.user_id LEFT JOIN class_divisions ON class_divisions.id=users.division LEFT JOIN schools ON schools.school_id=users.school_id WHERE (users.deactivated_at = '0000-00-00 00:00:00' OR users.deactivated_at IS NULL) AND users.role_id = 1 AND users.school_id = 2 AND teacher_student_conn.teacher_id = 10 AND
public function select_students() {
$query=("SELECT users.user_id, users.username, users.school_id, users.class, users.division, users.role_id, schools.school_name, schools.region,class_divisions.division FROM users
LEFT JOIN teacher_student_conn ON teacher_student_conn.student_id=users.user_id
LEFT JOIN teacher_student_conn as T ON T.teacher_id=users.user_id
LEFT JOIN class_divisions ON class_divisions.id=users.division
LEFT JOIN schools ON schools.school_id=users.school_id
WHERE (users.deactivated_at = '0000-00-00 00:00:00' OR users.deactivated_at IS NULL) AND users.role_id = 1 AND
users.school_id = 2 ");
$this->db->where('teacher_student_conn.student_id', NULL);
$result = $this->db->query($query);
echo $this->db->last_query();
return $result->result();
}
我找到了解決辦法:
$teacher=$this->uri->segment(3);
$query=("SELECT users.user_id, users.username, users.school_id, users.class, users.division, users.role_id, schools.school_name, schools.region,class_divisions.division FROM users
LEFT JOIN teacher_student_conn ON teacher_student_conn.student_id=users.user_id
AND teacher_student_conn.teacher_id = $teacher
LEFT JOIN teacher_student_conn as T ON T.teacher_id=users.user_id
LEFT JOIN class_divisions ON class_divisions.id=users.division
LEFT JOIN schools ON schools.school_id=users.school_id
WHERE (users.deactivated_at = '0000-00-00 00:00:00' OR users.deactivated_at IS NULL) AND users.role_id = 1 AND
users.school_id = 2 AND teacher_student_conn.student_id IS NULL");
$result = $this->db->query($query);
echo $this->db->last_query();
return $result->result();
}
請告訴我們,什麼是「不」工作?謝謝 –
它不顯示任何學生。它必須告訴我那些沒有這位老師的學生。我不確定我的查詢在包含「where」的部分中是否正確。 –
首先製作SQL語句,然後進行codeignitor查詢語法。 –