dongshungou7699 2014-04-03 13:12
浏览 19
已采纳

codeigniter查询问题

This code is working perfectaly in mysql run command

SELECT employeeCode
FROM employee_details
WHERE employeeCode
IN (
    SELECT DISTINCT (employeeCode) FROM quiz_answer_detailsWHERE submitTime
    IN (SELECT MIN( submitTime ) FROM quiz_answer_details WHERE quizId
    IN (SELECT id FROM quiz_details WHERE uploadtime =  '2014-04-03')
    AND answer IN (SELECT answer FROM quiz_details WHERE uploadtime =  '2014-04-03'))
   )

But I want to use this code on my codeigniter, but it is not working.

My codeigniter query code is

    $this->db->select('employeeCode');
    $this->db->from('employee_details');
    $this->db->where_in('employeeCode');
    $this->db->select('DISTINCT(employeeCode)');
    $this->db->from('quiz_answer_details');
    $this->db->where_in('submitTime');
    $this->db->select('min(submitTime)');
    $this->db->from('quiz_answer_details');
    $this->db->where_in('quizId');
    $this->db->select('id');
    $this->db->from('quiz_details');
    $this->db->where('uploadtime',"2014-04-03");
    $this->db->where_in('answer');
    $this->db->select('answer');
    $this->db->from('quiz_details');
    $this->db->where('uploadtime',"2014-04-03");
    $query=$this->db->get();
    print_r($query);
    if($query->num_rows>=1)
    {
        return $query;
    }
    else
    {
         return false;
    }

What is wrong please help me

  • 写回答

2条回答 默认 最新

  • doujupa7567 2014-04-03 13:34
    关注

    You should re write you subquery and use joins instead to get the better performance,without having full information regarding your tables/relationship and desired result i can't provide you the new query but you can use your subquery in active record's where function

    $subquery=" SELECT DISTINCT (employeeCode) FROM quiz_answer_detailsWHERE submitTime
        IN (SELECT MIN( submitTime ) FROM quiz_answer_details WHERE quizId
        IN (SELECT id FROM quiz_details WHERE uploadtime =  '2014-04-03')
        AND answer IN (SELECT answer FROM quiz_details WHERE uploadtime =  '2014-04-03')) ";
    $this->db->select('employeeCode');
    $this->db->from('employee_details');
    $this->db->where('employeeCode IN('.$subquery.')',null,FALSE);
    $query=$this->db->get();
    

    You should pass third parameter as FASLE in order to prevent the query to be quoted by bacticks Or you can use query() fucntion to run your raw queries

    $query=$this->db->query(' your full query here');
    $query->result();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码