Welcome Guest, Not a member yet? Register   Sign In
Select fields from different tables with ActiveRecord
#1

[eluser]Unknown[/eluser]
Hi,
I need to reply this query with activerecord:
'SELECT tab1.name1, tab2.name2 FROM tab1,tab2 WHERE tab1.id=tab2.id'

I know how to use join instead of WHERE:
$this->db->select('name1');
$this->db->from('tab1');
$this->db->join('tab2', 'tab1.id=tab2.id');

... but I need the tab2.name2 value, and I can't add another table to 'from'.

Any help ??
#2

[eluser]Phil Sturgeon[/eluser]
Give them a nice short alias, then use the alias in your select line.

Code:
$this->db->select('t1.*, t2.something');
$this->db->from('some_table t1');
$this->db->join('stuff t2', 't1.id=t2.id');

That selects everything from some_table and something from stuff (t2)
#3

[eluser]José Wilker[/eluser]
Hello, you can use the alias in several conditions.

$this->db->from("table a");

or

$this->db->join("table t","t.field=table.field");

after you insert the table name, you can add space and insert the alias.
you can use alias in conditions with agregate more tables or a unique table.


JW
#4

[eluser]Phil Sturgeon[/eluser]
Correct me if I am wrong, but is that not exactly what I just said? Smile
#5

[eluser]José Wilker[/eluser]
That's right, you're right, only that which was supplemented for added table name in the active record you can insert a space and add an alias.




Theme © iAndrew 2016 - Forum software by © MyBB