带有活动记录的codeigniter中的嵌套查询

时间:2015-10-10 13:30:14

标签: php mysql codeigniter

此查询在原始php中正常工作。但是我需要在codeigniter中使用活动记录,因为下面的模型不起作用。在浏览器中显示语法错误。

SELECT m.no, m.Module, m.Duration 
FROM `tbl_module`m
WHERE round=(SELECT Round FROM `tbl_assigned` WHERE TraineeID='".$TID."') 
AND   trade=(SELECT tbl_Trade.ID FROM `tbl_Trade`, `tbl_assigned`
            WHERE `tbl_Trade`.ID=`tbl_assigned`.Trade AND TraineeID='".$TID."');

型号:

public function get_Module($TID) {
    $this->db->select('*');
    $this->db->from('tbl_trade AS t');
    $this->db->join('tbl_assigned AS a', 't.id=a.trade', 'INNER');
    $this->db->where('TraineeID', $TID);
    $where_clause1 = $this->db->get_compiled_select();

    $this->db->select('*');
    $this->db->from('tbl_assigned AS a');
    $this->db->join('tbl_trade AS t', 't.id=a.trade', 'INNER');
    $this->db->where('TraineeID', $TID);
    $this->db->where("`trade`, ($where_clause1)", null, false);
    $where_clause2 = $this->db->get_compiled_select();

    $this->db->select('*');
    $this->db->from('tbl_module');
    $this->db->where("`round`, ($where_clause2)", null, false);
    $query = $this->db->get();
    $data = $query->result();

    return $data;
}

0 个答案:

没有答案