如何在codeigniter中的ajax数据中使用or_where和where条件

时间:2017-09-15 05:30:52

标签: codeigniter datatables

如何在OR条件下得到和条件。在OR之后使用的条件不起作用。

$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)加入otest_subjects ON otest_questionsqst_subject_id = otest_subjectssubject_id加入otest_main_category开启otest_subjectssubject_main_cat_id = otest_main_categorymain_cat_id加入otest_sub_category ON otest_main_categorymain_cat_id = otest_sub_category。{{ 1}} WHERE sub_cat_main_cat_idotest_questions ='12'和qst_added_byotest_questions ='988'和qst_chapter_idotest_questions ='988'或qst_chapter_idotest_questions = 1 GROUP BY access_levelotest_questions

但查询应该是

WHERE qst_idotest_questions ='12'和qst_added_byotest_questions ='988'或qst_chapter_idotest_questions = 1 AND access_levelotest_questions ='988'

2 个答案:

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

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