MySQL在codeigniter中加入两个表查询

时间:2016-07-21 07:08:38

标签: php mysql codeigniter

我有两个表,限制和交易 enter image description here enter image description here

        $wherehis= array('status'=>'Yes','company_id' =>$compnayid);
        $this->db->select('type,tra.amount,tra.qty,limit_id');
        $this->db->where($wherehis);
        $this->db->limit(100);
 $this->db->order_by('tra.tra_id','DESC');
        $this->db->join('limit_history as sell', 'tra.sell_limit_id = sell.limit_id','left');
        $this->db->join('limit_history as buy', 'tra.buy_limit_id = buy.limit_id','left');
        $data['Histroy']= $this->db->get("tbltrancation as tra")->result_array(); 

但只获得销售订单....请帮助我thanx

获得买卖两种数据

enter image description here

2 个答案:

答案 0 :(得分:0)

试试这个,

    $this->db->limit(100);
    $this->db->order_by('tra.tra_id','DESC');
    $this->db->join('limit_history as sell', '(tra.sell_limit_id = sell.limit_id AND tra.buy_limit_id = buy.limit_id)','left');
    $data['Histroy']= $this->db->get("tbltrancation as tra")->result_array();

答案 1 :(得分:0)

如果列名相同,则需要为它们提供别名以区分它们:

$this->db
    ->select('tra.*, sell.amount as sell_amount, sell.qty as sell_qty, buy.amount as buy_amount, buy.qty as buy_qty')
    ->from('tbltrancation as tra')
    ->join('limit_history as sell', 'tra.sell_limit_id = sell.limit_id', 'left')
    ->join('limit_history as buy', 'tra.buy_limit_id = buy.limit_id', 'left')
    ->order_by('tra.tra_id', 'desc')
    ->limit(100);

$data['Histroy'] = $this->db->get()->result_array();