11

I'm new to building databases and I'm trying to do a JOIN based on a having three database tables.

Table A = ID, Name, etc
Table B = ID, Name, etc
Table C = ID, TableAId, TableBId

How can I JOIN these tables with query builder methods? I'm trying to make as few requests as possible, but am getting stumped on how it should all be written without doing three separate calls.

1
  • This pseudo schema isn't very clear about the desired query. Commented Nov 18 at 10:37

4 Answers 4

35
$this->db->select('*');
$this->db->from('TableA AS A');// I use aliasing make joins easier
$this->db->join('TableC AS C', 'A.ID = C.TableAId', 'INNER');
$this->db->join('TableB AS B', 'B.ID = C.TableBId', 'INNER');
$result = $this->db->get();

The join function works like this: join('TableName', 'ON condition', 'Type of join');

The equivilent sql:

SELECT *
FROM TableA AS A
    INNER JOIN TableC AS C
    ON C.TableAId = A.ID
    INNER JOIN TableB AS B
    ON B.ID = C.ID

I found that writing the SQL first, testing it, then converting to the active record style minimizes error.

Sign up to request clarification or add additional context in comments.

2 Comments

This produces duplicate rows. Any solution?
@JWCMay It will never duplicate rows, you have multiple matches, which is the entire purpose. You should redesign your database if you want different data.
5
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$this->db->join('authors', 'authors.id = comments.author_id');

hopefully you get my example.

Just add another $this->db->join();

For complex queries you might be better off looking at an ORM such as doctrine

Comments

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

$this->db->from('table1');

$this->db->join('table2','table1.id=table2.id'); 

$this->db->join('table3','table2.id=table3.id');

$this->db->join('table4','table3.id=table4.id'); 

$this->db->join('table5','table5.id=table4.id');

$this->db->where('table5.id',$this->session->userdata('id'));//getting value from session and match the id of table5 and then show data

$data=$this->db->get()->result();//all data store in $data variable

1 Comment

This answer is missing its educational explanation.
0

if you want a flexible query you could use:

http://codeigniter.com/user_guide/database/results.html

which utilizes the following syntax $query = $this->db->query('SELECT * FROM my_table');

here is the query:

SELECT a.name as namea ,b.name as nameb FROM tablec c
JOIN tablea a ON a.ID = c.ID
JOIN tableb b ON b.ID = c.ID

you may want to read more about joins here

then go through your results in such a way:

$query = $this->db->query("YOUR QUERY");

foreach ($query->result_array() as $row)
{
   echo $row['namea'];
   echo $row['nameb'];
}

1 Comment

using the query command is just a quick way of getting out of the problem, this doesn't solve anything for him. Also there are potential security risks because individual data won't be escaped by CI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.