如何以活动记录形式编写正常的sql查询

时间:2015-08-05 06:56:07

标签: mysql sql codeigniter activerecord

$ba = $this->db->query("select * from result, users, exam_title, states, user_zone, detailed_result, answers
            WHERE users.user_id = result.user_id AND exam_title.title_id = result.exam_id AND users.state = states.state_id AND
            users.user_zone = user_zone.user_zone_id AND detailed_result.given_ans_id = answers.ans_id 
            AND detailed_result.ques_id = answers.ques_id AND users.user_id = detailed_result.user_id AND
            result.exam_id = '".$exid."' AND detailed_result.ques_id = '".$qid."' ")
            ->result();

如何在codeignitor活动记录表单中编写上述sql查询

我尝试使用下面的代码,但显示错误

 $ba = $this->db->select('*')
            ->from('result')
            ->where('detailed_result.ques_id', $qid)
            ->where('result.exam_id', $exid)

            ->where('answer.ans_id', 'detailed_result.given_ans_id')

            ->where('users.user_id', 'detailed_result.user_id')

            ->join('users', 'users.user_id = result.user_id', 'left')
            ->join('exam_title', 'exam_title.title_id = result.exam_id', 'left')
            ->join('states', 'users.state = states.state_id', 'left')
            ->join('user_zone', 'users.user_zone = user_zone.user_zone_id', 'left')

            ->join('detailed_result', 'result.exam_id = detailed_result.exam_id', 'left')
            ->join('answers', 'detailed_result.ques_id = answers.ques_id', 'left')
            ->get()
            ->result();

2 个答案:

答案 0 :(得分:0)

如下所示使用它,如果有任何错误,请告诉我。

       $ba = $this->db->select('*')
        ->from('result')

        ->join('users', 'users.user_id = result.user_id', 'left')
        ->join('exam_title', 'exam_title.title_id = result.exam_id', 'left')
        ->join('states', 'users.state = states.state_id', 'left')
        ->join('user_zone', 'users.user_zone = user_zone.user_zone_id', 'left')

        ->join('detailed_result', 'result.exam_id = detailed_result.exam_id', 'left')
        ->join('answers', 'detailed_result.ques_id = answers.ques_id', 'left')
         ->where('detailed_result.ques_id', $qid)
        ->where('result.exam_id', $exid)

        ->where('answers.ans_id', 'detailed_result.given_ans_id')

        ->where('users.user_id', 'detailed_result.user_id')

        ->get()
        ->result();

答案 1 :(得分:0)

试试这段代码

 $ba = $this->db->select('*')
        ->join('users', 'users.user_id = result.user_id', 'left')
        ->join('exam_title', 'exam_title.title_id = result.exam_id', 'left')
        ->join('states', 'users.state = states.state_id', 'left')
        ->join('user_zone', 'users.user_zone = user_zone.user_zone_id', 'left')
        ->join('detailed_result', 'result.exam_id = detailed_result.exam_id', 'left')
        ->join('answers', 'detailed_result.ques_id = answers.ques_id', 'left')

        ->where('detailed_result.ques_id', $qid)
        ->where('result.exam_id', $exid)
        ->where('answer.ans_id', 'detailed_result.given_ans_id')
        ->where('users.user_id', 'detailed_result.user_id')
        ->get('result');

然后从查询中获取结果数组

$result = $ba->result_array();