0

Basically I have a db_select that has two left joins. And will be calling up two table fields with the same name. The fields have different data though, so I need to be able to have access to both in my results. In a normal query I’d set this in the SELECT - how can i do the same in db_select

SELECT table1.column AS column1, table2.column AS column2
FROM table1
LEFT JOIN table2
ON table1.column = table2.column

here's my code that i need to change

$query->leftjoin('migrate_map_emp', 'm', 'm.sourceid1 = i.ID');
$query->leftjoin('migrate_map_emplogo', 'ml', 'ml.sourceid1 = i.ID');

$query->fields('i', array('ID','Name'))
      ->fields('m', array('destid1'))
      ->fields('ml', array('destid1'));

[solved]

the above code works. I did a 'drush_print_r' on the rows returned at the second item is labelled ml_

[destid1] => 5 [ml_destid1] => 2

Many thanks

2
  • You can also use the addField method to specify an aliased name name.
    – Nels
    Commented Jun 24, 2014 at 11:12
  • All the join methods return the new alias, you should use that for 'best practice' (potential collisions with other provided aliases are handled)
    – Clive
    Commented Jun 24, 2014 at 12:28

1 Answer 1

0

You can fetch data with drupal db_select as below code:-

$query= db_select('table1', 'column1');
$query->join('table2', 'column2', 'column1.column=column2.column'); 

$query=$query->fields('column1',array('column'))
       ->fields('column2',array('column'))       
       ->execute()->fetchAll();

For more information on db_select join queries click on this link

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.