2017-09-15 38 views
0

如何獲取和條件在OR條件。 OR操作失敗後使用的條件。如何使用or_where和where code codeigniter中的ajax數據表中的條件

$this->datatables->select('otest_questions.qst_id,otest_sub_category.sub_cat_name,otest_subjects.subject_display_name,otest_questions.qst_question,otest_questions.qst_status'); 
     $this->datatables->join('otest_subjects',' otest_questions.qst_subject_id=otest_subjects.subject_id'); 
     $this->datatables->join('otest_main_category','otest_subjects.subject_main_cat_id=otest_main_category.main_cat_id'); 
     $this->datatables->join('otest_sub_category','otest_main_category.main_cat_id=otest_sub_category.sub_cat_main_cat_id'); 
     $this->datatables->where(array('otest_questions.qst_added_by'=>$this->session->userdata['inst_in']['inst_id'],'otest_questions.qst_chapter_id'=>$this->input->get('chapter'))); 
     $this->datatables->or_where(array('otest_questions.access_level'=>1)); 
     $this->datatables->where(array('otest_questions.qst_chapter_id'=>$this->input->get('chapter'))); 

     $this->datatables->group_by('otest_questions.qst_id'); 
     $this->datatables->from('otest_questions'); 
     } 

     echo $this->datatables->generate(); 

執行的查詢是

SELECT * FROM(otest_questions)JOIN otest_subjects ON otest_questionsqst_subject_id = otest_subjectssubject_id JOIN otest_main_category ON otest_subjectssubject_main_cat_id = otest_main_categorymain_cat_id JOIN otest_sub_category ON otest_main_categorymain_cat_id = otest_sub_categorysub_cat_main_cat_id其中otest_questionsqst_added_by ='12'和otest_questionsqst_chapter_id ='988'和otest_questionsqst_chapter_id ='988'或otest_questionsaccess_level = 1 GROUP BY otest_questionsqst_id

但查詢應該是

WHERE otest_questionsqst_added_by ='12'和otest_questionsqst_chapter_id ='988'或otest_questionsaccess_level = 1 AND otest_questionsqst_chapter_id = '988'

+0

'group_by'需要'爲了by'條款以及 –

+0

or_where應檢查同一個數據庫列 –

回答

0
$inst_id = $this->session->userdata['inst_in']['inst_id']; 
$chapter = $this->input->get('chapter'); 

$query = "SELECT * FROM (otest_questions) 
JOIN otest_subjects ON otest_questions.qst_subject_id=otest_subjects.subject_id 
JOIN otest_main_category ON otest_subjects.subject_main_cat_id=otest_main_category.main_cat_id 
JOIN otest_sub_category ON otest_main_category.main_cat_id=otest_sub_category.sub_cat_main_cat_id 
WHERE (otest_questions.qst_added_by = '$inst_id' AND otest_questions.qst_chapter_id = '$chapter') 
OR (otest_questions.qst_chapter_id = '$chapter' AND otest_questions.access_level = 1) 
GROUP BY otest_questions.qst_id"; 

$this->datatables->query($query); 
+0

,我想利用和條件or_where。 – Deepa

0
$query = "(SELECT a.qst_id,d.sub_cat_name,b.subject_display_name,a.qst_question,a.qst_status from otest_questions as a JOIN otest_subjects as b ON a.qst_subject_id = b.subject_id JOIN otest_main_category as c ON b.subject_main_cat_id = c.main_cat_id JOIN otest_sub_category as d ON c.main_cat_id = d.sub_cat_main_cat_id WHERE a.qst_added_by =".$this->session->userdata['inst_in']['inst_id'] ." AND a.qst_chapter_id =".$this->input->get('chapter')." OR a.access_level = 1 AND a.qst_chapter_id =".$this->input->get('chapter')." GROUP BY a.qst_id) as F"; 
    $this->datatables->select('*'); 
    $this->datatables->from($query); 
    echo $this->datatables->generate(); 
相關問題