尝试从三个查询的组合中获取数据时出错

时间:2018-02-15 05:50:21

标签: php mysql codeigniter

大家好我正在尝试从三个查询的组合中获取数据,但收到的错误如下:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   near' SELECT * FROM object_data a JOIN object_reference b ON a.obj_id   = b.obj_id加入'在第1行。

我的任务是在用户,课程和测试的组合中获取数据。     但我没有得到数据。三个查询在database.inow中完全独立运行。我的代码请查看并帮助我。

控制器:

public function getReports()
{
    $records            = [];
    $data               = [];
    $data['title']      = 'test Report'; 
    $date_type          = $this->input->get('filter_options');
    $course             = $this->input->get('course');
    $filter_options     = $date_type;
    $department         = $this->input->get('department');
    $division           = $this->input->get('division');
    $month              = 1;
    $year               = date('Y');
    $day                = date('d');
    $data['department'] = $department;
    $data['course']     = $course;
    $data['division']   = $division;
    $data['date_type']  = $date_type;
    $course_condition   = ' AND 1=1 ';

    if($course)
        $course_condition = ' AND o.title="'.$course.'"';

    $department_condition = ' AND 1=1 ';

    if($department)
        $department_condition = ' AND u.department="'.$department.'"';

    $date_condition = ' and 1=1 ';

    if($date_type==1)
    {
        $date_condition = ' and ta.submittimestamp BETWEEN DATE_SUB(NOW(), INTERVAL 300 DAY) AND NOW() ';
    }
    else if($date_type==2)
    {
        $date_condition = ' and MONTH(ta.submittimestamp) = MONTH(CURRENT_DATE()) AND YEAR(ta.submittimestamp) = YEAR(CURRENT_DATE()) ';  
    }
    else if($date_type==3)
    {
        $year           = $this->input->get('year');
        $month          = $this->input->get('month');
        $date_condition = '  and YEAR(ta.submittimestamp) = "'.$year.'" and MONTH(ta.submittimestamp) = "'.$month.'" '; 
    }

    $group_condition = "GROUP by u.usr_id";

    if ($course) 
        $users_query = "SELECT o.title as course_title,u.*,u.usr_id,o.obj_id FROM object_data o,usr_data u,obj_members rn WHERE o.obj_id=rn.obj_id AND u.usr_id=rn.usr_id AND o.type='crs'  AND o.obj_id=$course";

    $user_records = $this->base_model->executeSelectQuery($users_query);

    $final_data = [];

    foreach($user_records as $user)
    {
        if(!$course)
            continue;

        $user_course_query ="SELECT  * FROM object_data a JOIN object_reference b ON a.obj_id = b.obj_id JOIN tree c ON b.ref_id = c.parent JOIN object_reference d ON c.child = d.ref_id JOIN tree e ON d.ref_id = e.parent JOIN object_reference f ON e.child = f.ref_id JOIN object_data g ON f.obj_id = g.obj_id and a.obj_id=$course ";

        $user_course_records = $this->base_model->executeSelectQuery($user_course_query);

        foreach($user_course_records as $course_data)
        { 
            $user_tests_query = "SELECT u.firstname,o.*,ta.*,tpr.workingtime,tcr.mark_official,(tcr.reached_points/tcr.max_points)*100 as result FROM usr_data u,object_data o,tst_active ta,tst_tests tt ,tst_pass_result tpr, tst_result_cache tcr WHERE u.usr_id=ta.user_fi AND tt.obj_fi=o.obj_id AND ta.test_fi=tt.test_id  AND ta.active_id=tpr.active_fi AND tcr.active_fi=ta.active_id       AND u.usr_id=$user->usr_id AND  o.obj_id = $user_course_query ".$date_condition;

            $user_tests = $this->base_model->executeSelectQuery($user_tests_query);

            foreach($user_tests as $test)
            {
                $dta['usr_id']          = $user->usr_id;
                $dta['firstname']       = $user->firstname;
                $dta['email']           = $user->email;
                $dta['matriculation']   = $user->matriculation;
                $dta['approve_date']    = $user->approve_date;
                $dta['department']      = $user->department;
                $dta['tries']           = $test->tries;
                $dta['test_title']      = $test->title;
                $dta['submittimestamp'] = $test->submittimestamp;
                $dta['workingtime']     = $test->workingtime;
                // $dta['mark_official']     = $test->mark_official;
                $dta['result']          = $test->result;

                if($test->mark_official=='passed')
                    $result_status = '<span class="label label-primary"> Passed </span>';
                else if($test->mark_official=='failed')
                    $result_status = '<span class="label label-danger"> Failed </span>';

                $dta['status']    = $result_status;
                $dta['division']  = $this->udfTextData(2);
                $dta['job_title'] = $this->udfTextData(6);
                $final_data[]     = $dta;
            }
        }

    }
    $data['records'] = $final_data;

但未能获得数据。三个查询在数据库中单独运行。我编写了上面的代码来获取用户,测试和课程组合中的数据无法使数据超出错误

0 个答案:

没有答案