使用内部联接codeigniter查询4个表

时间:2013-02-21 10:15:05

标签: codeigniter activerecord

我有4个工作岗位,公司,就业类型& job_category每个的主键是job_id,com_id,type_id,job_cat_id,但是(com_id,type_id,job_cat_id)是作业表的外键。

我没有活动记录的查询工作完美,如下所示

select company.com_id, company.company_name, jobs.job_id, jobs.title, jobs.opening_date, jobs.closing_date, jobs.number_of_pos, employment_type.type_id, employment_type.type, job_category.job_cat_id,  job_category. category from company inner join jobs on company.com_id=jobs.com_id inner join employment_type on employment_type.type_id=jobs.type_id inner join job_category on job_category.job_cat_id=jobs.job_cat_id

但如果我尝试使用codeiginiter活动记录,例如

  $this->db->select('company.com_id, company.company_name, jobs.job_id, jobs.title, jobs.opening_date, jobs.closing_date, jobs.number_of_pos, employment_type.type_id, employment_type.type, job_category.job_cat_id,  job_category. category');
            $this->db->from('company');
            $this->db->join('jobs','company.com_id=jobs.com_id','inner');
            $this->db->join('employment_type', 'employment_type.type_id=jobs.type_id','inner');
            $this->db->join('job_category', 'job_category.job_cat_id=jobs.job_cat_id','inner');
            $this->db->order_by('job_id','DESC');
            $this->db->limit($limit, $offset);
            $query = $this->db->get();
            return $query->result_array();

我最终得到以下错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`) INNER JOIN `jobs` ON `company`.`com_id`=`jobs`.`com_id` INNER JOIN `employmen' at line 1

SELECT `company`.`com_id`, `company`.`company_name`, `jobs`.`job_id`, `jobs`.`title`, `jobs`.`opening_date`, `jobs`.`closing_date`, `jobs`.`number_of_pos`, `employment_type`.`type_id`, `employment_type`.`type`, `job_category`.`job_cat_id`, `job_category`.` category FROM (`company`) INNER JOIN `jobs` ON `company`.`com_id`=`jobs`.`com_id` INNER JOIN `employment_type` ON `employment_type`.`type_id`=`jobs`.`type_id` INNER JOIN `job_category` ON `job_category`.`job_cat_id`=`jobs`.`job_cat_id` ORDER BY `job_id` DESC LIMIT 10

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

你在job_category. category中有一个空格可以移除它,你应该是金色的

答案 1 :(得分:0)

像这样格式化您的查询

$data   =   array(
                'company.com_id', 
                'company.company_name', 
                'jobs.job_id', 
                'jobs.title',
                'jobs.opening_date', 
                'jobs.closing_date', 
                'jobs.number_of_pos', 
                'employment_type.type_id', 
                'employment_type.type', 
                'job_category.job_cat_id',  
                'job_category.category'
);

$this->db->select($data);
$this->db->from('company');
$this->db->join('jobs','company.com_id=jobs.com_id','inner');
$this->db->join('employment_type', 'employment_type.type_id=jobs.type_id','inner');
$this->db->join('job_category', 'job_category.job_cat_id=jobs.job_cat_id','inner');
$this->db->order_by('job_id','DESC');
$this->db->limit($limit, $offset);
$query = $this->db->get();
return $query->result_array();