我有几张桌子,希望一次显示一个id。
id | isi a1 | isi a2
id | id_a | isi b1 | isi b2
id | id_a | isi c1 | isi c2
id | id_a | isi d1 | isi d2
id | id_a | isi e1 | isi e2
我想显示isi B1-E2,其中每个表在表A中都有id_a = id。我不知道mysql join,我试过这段代码
$this->db->select('BaseTbl.id, BaseTbl.tanggal, BaseTbl.atas_nama, BaseTbl.kerugian, BaseTbl.keterangan, BaseTbl.admin, BaseTbl.status');
$this->db->from('data_blacklist as BaseTbl');
$this->db->join('bl_rekening as Rekening, bl_telefon as Telefon, bl_bukti as Bukti, bl_pelapor as Pelapor', 'Rekening.id_blacklist = BaseTbl.id, Telefon.id_blacklist = BaseTbl.id, Bukti.id_blacklist = BaseTbl.id, Pelapor.id_blacklist = BaseTbl.id','left');
但它总是给我
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 ' bl_telefon as Telefon, bl_bukti as Bukti, bl_pelapor as Pelapor ON `Rekening`.`' at line 3
我现在不知道。任何的想法?我使用codeigniter
答案 0 :(得分:2)
试试这个。您必须查询每个表的连接。你无法将它们组合起来
$this->db->select('BaseTbl.id, BaseTbl.tanggal, BaseTbl.atas_nama, BaseTbl.kerugian, BaseTbl.keterangan, BaseTbl.admin, BaseTbl.status');
$this->db->from('data_blacklist as BaseTbl',);
$this->db->join('bl_rekening as Rekening','Rekening.id_blacklist = BaseTbl.id','left');
$this->db->join('bl_telefon as Telefon','Telefon.id_blacklist = BaseTbl.id','left');
$this->db->join('bl_bukti as Bukti','Bukti.id_blacklist = BaseTbl.id','left');
$this->db->join('bl_pelapor as Pelapor','Pelapor.id_blacklist = BaseTbl.id','left');
答案 1 :(得分:1)
$this->db->select('a.*,b.*,c.*,d.*,e.*');
$this->db->from( TABLES::$A . ' AS a' );
$this->db->join(TABLES::$B . ' AS b', 'a.id = b.id_a');
$this->db->join(TABLES::$C . ' AS c', 'a.id = c.id_a');
$this->db->join(TABLES::$D . ' AS d', 'a.id = d.id_a');
$this->db->join(TABLES::$E . ' AS e', 'a.id = e.id_a');
$query=$this->db->get();
// echo $this->db->last_query();
$result = $query->result_array();
return $result;
答案 2 :(得分:0)
我可以帮助您在CodeIgniter中进行手动查询。
return $this->db->query("select A.*, B.*, C.*, D.*, E.* from Table A, Table B, Table C, Table D, Table E WHERE A.id = B.id_a AND A.id = C.id_a AND A.id = D.id_a AND A.id = E.id_a")->result_array();
A,B,C,D和E是表的别名。如果值存在,它将为您带来数据。或者您也可以编写手动查询