Reputation: 3
I have two tables.
Table One
xx
yy
zz
Table Two
xx-3
xx-2
xx-1
yy-2
yy-1
zz-4
zz-3
zz-2
zz-1
I want to use join between these 2 tables in such a way that it will use latest record from Table Two for each record of table One. i.e. xx join with xx-3 yy join with yy-2 and zz join with zz-4
query will go like this:
$query = db_select('One', 'o');
$query->leftJoin('Two', 't', 't.column1 = o.column1');
$query->fields('o',array('column1','column2'));
$query->fields('t',array('column1','column2'));
$query->orderBy('o.column1', 'DESC');
How can I do this using db_select in Drupal 7?
Upvotes: 0
Views: 1250
Reputation: 21681
Try this code
$query = db_select('Two', 't');
$query->join('One', 'o', 'o.column1 = t.column1');
$query->fields('t',array('column1','column2'));
$query->orderBy('t.column1', 'DESC');
$res = $query->execute()->fetchAll();
Thanks!
Upvotes: 0