Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JOIN query causes column name collisions in SELECT * clause [duplicate]

I have 2 tables in my database which I need to join. 1 table is the artikelen table and the other one is the collecties table. I currently have.

$this->db->select('*');
$this->db->from('collecties');
$this->db->join('artikelen', 'artikelen.collecties_id = collecties.id');

It gives the right result but all the double fields (collecties has a title field and artikelen has a title field) will become one (it returns the artikelen.title field), and I can't access the row of the other table (the collecties.title field).

I select 10 fields from artikelen and only collecties.title from collecties.

What is the simples way to do this without having to replace

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

with all the 10 fields with an as statement.

like image 204
Sven van den Boogaart Avatar asked Sep 06 '25 02:09

Sven van den Boogaart


1 Answers

Make sure your both table got rows on your joining condition , otherwise it will return null. and modify the select as follows
$this->db->select('artikelen.*,collecties.title as ctitle');

like image 169
Fisherman Avatar answered Sep 07 '25 20:09

Fisherman