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条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集