[英]passing array in where clause in join
我在CI內部有一個自定義查詢,該自定義查詢從兩個表中獲取多個數據我正在實現一種搜索機制,其中使用get請求來解析多個參數,我想自定義它們的查詢以基於搜索參數來獲取數據!
這是我的查詢,正在運行,它不包含任何where子句
return $this->db->query('Select t1.id,t1.course_name,t1.course_duration,t1.course_price,t1.course_category, t2.first_name , t2.last_name , t2.email
from teacher_courses as t1 LEFT JOIN teacher as t2 on t1.teacher_id=t2.id
')->result_array();
現在查詢將保持完全相同,我只想根據where子句中的search參數輸入檢索數據
這是我的網址格式
http://localhost/online-learning/Home/courses?courses=1-3&teacher=1
在我的控制器內,我正在做這樣的事情
if(isset($_GET['courses'])){
(isset($_GET['courses'])) ? $get['courses']=explode('-', $_GET['courses']) :"";
(isset($_GET['teacher'])) ? $get['teacher']=explode('-', $_GET['teacher']) :"";
echo "<pre>";
print_r($get);
}
現在以以下形式檢索數據
Array
(
[courses] => Array
(
[0] => 1
[1] => 3
)
[teacher] => Array
(
[0] => 1
)
)
因此,如何在我的聯接查詢的where子句中傳遞此數組。
$courses = implode(',', $array['courses']);
$teacher = implode(',', $array['teacher']);
return $this->db->query('Select
t1.id,t1.course_name,t1.course_duration,t1.course_price,t1.course_category,
t2.first_name , t2.last_name , t2.email
from teacher_courses as t1
LEFT JOIN teacher as t2 on t1.teacher_id=t2.id WHERE course_id IN ($courses) AND teacher_id IN ($teacher)')->result_array();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.