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

ao218c7q  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(323)

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

如何根据客户id将表2(两者)receiver和seller显示为name?
我试过:

$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”附近使用正确的语法
我查了其他类似的问题,但还是想不通。

6l7fqoea

6l7fqoea1#

对于这样的多列查询,连接同一个表

$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();

相关问题