Select with Join, in Codeigniter

0

I'm having a doubt on a select with the use of join in codeigniter, it follows:

I have 2 tables.

tabela jogo
id | id_time1 | id_time2
99 |    1     |    2

tabela time
id | time
1  | Real
2  | Barcelona

I want to return teams to set up a showdown: Real x Barcelona

My select looks like this:

$this->db->select('jogo.*, time.time AS time_nome1, time.time AS time_nome2');
$this->db->from('jogo');   
$this->db->join('time', 'time.id = jogo.id_time1');     

This way I can return team 1 but not team 2 or vice versa by changing the join to juego.id_time2

How do I join to return both teams?

Thanks in advance

    
asked by anonymous 22.01.2016 / 02:35

1 answer

3

A Join has been missing, the code follows:

$this->db->select('jogo.*, t1.time AS time_nome1, t2.time AS time_nome2');
$this->db->from('jogo');
$this->db->join('time as t1', 't1.id = jogo.id_time1');
$this->db->join('time as t2', 't2.id = jogo.id_time2');

The names of the teams are in the same table, however each team has an ID, so each team is a row in the table. To bring the two at the same time, you have to do two inner joins. Note that I used t1 and t2 as aliases and referenced these aliases in select (t1.time and t2.time). Hope this helps.

    
22.01.2016 / 11:16