Codeigniter连接表并显示名称而不是id

时间:2018-05-23 10:30:14

标签: mysql codeigniter join

-------------------------------
table 1 (customer) : id , name
table 2 (transaction): receiver(int), seller(int)
---------------------------------------------------

如何根据客户ID显示表2(两者)接收者和卖家名称?

我试过了:

    $this->db->select('transactions.*');


    $this->db->select("customer.id AS c_id");
    $this->db->select("customer.name AS c_name");
    $this->db->from("customer"); //edited
    $this->db->join('transactions', 'customer.id = transactions.receiver');
    $this->db->join('transactions', 'customer.id = transactions.sender = customer.id');
    $query = $this->db->get();
    return $query->result();

但它给了我错误:

  

您的SQL语法有错误/请查看与您的MariaDB服务器版本对应的手册,以便在' JOIN'

附近使用正确的语法

我检查了其他类似的问题,但仍然无法弄清楚。

1 个答案:

答案 0 :(得分:0)

用于连接同一个表以进行多列查询,如此

$this->db->select('transaction.*,tbl_receiver.name as receiver_name,tbl_sender.name as sender_name');
$this->db->join('customer as tbl_receiver','tbl_receiver.id=transaction.receiver');
$this->db->join('customer as tbl_sender','tbl_sender.id=transaction.sender');
return $this->db->get('transaction')->result_array();