Select fields from two tables using db_select() I am using db_select() and I don't understand the syntax of the fields() method. I'm using a join() to join another table. So for tables t and n, I want to do something like

SELECT t.tid, t.field1, t.field2, n.title, n.author 
FROM table t INNER JOIN node n ON t.nid = n.nid;


I have

$results = db_select('table', 't')
  -&gt;fields( ... )
  -&gt;join('node', 'n', 'n.nid = t.nid')
  -&gt;execute();


But as you can see, I'm lost at fields(). The examples I've seen only specify fields for one table:

-&gt;fields('t', array('tid', 'field1', 'field2'))


What is the syntax that I want to use?
Easy, just call fields() twice.

$query = db_select('table', 't');
$query-&gt;join('node', 'n', 'n.nid = t.nid');
$result = $query
  -&gt;fields('t', array('tid', 'field1', 'field2'))
  -&gt;fields('n', array('nid', 'field1', 'field2'))
  -&gt;execute();

foreach ($result as $row) {
  // Do something with $row.
}


You can call most methods multiple times (multiple fields, multiple conditions, multiple sort orders, multiple joins, ...).

Note that, as shown in my example, the call to join() needs to be separate and can not be "chained" (that's the technical term for having multiple method calls in a row) because it does not return the query object but the name for the table alias.