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

1

4

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.

Memphian answered 9/4, 2014 at 17:34 Comment(4)
not sure why its closed, related question is not related, this is not about the query but about codeignitor ...the underlying probably doesn't pertain to CodeIgniter, this is a misunderstanding about how to avoid column collisions. The fact that you encountered this common problem while using CodeIgniter makes this page a potentially valuable signpost for the dupe target which explains how to resolve the problem.Nonfiction
@Nonfiction my point was that the query was correct, but because there is not data it return null, this is a codeignitor thing, it uses that is sql correct ,but the problem was the return value that i assumed to be an empty array.Memphian
Your asked question contains no code which calls ->get() nor any result set forming method. Whatever new problem you are experiencing, it will not be resolved by the accepted answer if not also solved by the dupe targets. Your question is currently either a duplicate or Unclear.Nonfiction
@Nonfiction on second look i think you are right, my bad.Memphian
I
8

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');

Idolize answered 9/4, 2014 at 17:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.