Although most SELECT queries are static, at times we will need a more flexible query. That could be because the query itself may change depending on incoming user data, because we want to allow other modules to modify our query before it is executed, or we want to take advantage of some database feature that is implemented differently on different databases. For these cases, Drupal provides a mechanism for building dynamic queries using a robust query builder.
To start, we create a new query object with
$query = db_select('node', 'n');
The first parameter is the name of the base table of the query and the second is the alias we want to use for it. We then call additional methods on the
$query object in order to build ...