select query
Select
----------------------------------------
db_select($table, $alias = NULL, $options = array())
$table Database table to select from.
$alias Table alias.
return New query object.
$query = db_select('users', 'u') ->fields('u', array('uid', 'name')); $result = $query->execute();
----------------------------------------
->distinct($distinct = TRUE)
$distinct Flag indicating DISTINCT query.
return The called query object.
----------------------------------------
->fields($table_alias, $fields = array())
$table_alias Alias of the table the field belongs to.
$fields Array of field names.
return The called query object.
----------------------------------------
->addField($table_alias, $field, $alias = NULL)
$table_alias Alias of the table the field belongs to.
$field Field name. $alias Field alias.
return Unique field alias.
----------------------------------------
->range($start = NULL, $length = NULL)
$start First record of the result set.
$length Max number of records.
return The called query object.
----------------------------------------
->groupBy($field)
$field The field to group by.
return The called query object.
----------------------------------------
->orderBy($field, $direction = 'ASC')
$field The field to order by.
$direction 'ASC' or 'DESC'.
return The called query object.
----------------------------------------
->orderRandom()
return The called query object.
----------------------------------------
->union(SelectQueryInterface $query,$type = '')
$query Query to union.
$type Type of union.
return New resulting query object.
----------------------------------------
->addExpression($expression, $alias= NULL, $arguments = array())
$expression Expression string.
$alias Expression alias.
$arguments Assoc array of placehoders and placeholder values.
return Unique expression alias.
----------------------------------------
->countQuery()
return New query object.
----------------------------------------
->addTag($tag)
$tag Query identification.
return The called query object.
----------------------------------------
->hasTag($tag)
$tag Query identification.
return TRUE if condition is met.
----------------------------------------
COUNT EXAMPLE
---------------------------------
$results = db_select('table')
->fields(NULL, array('field'))
->countQuery()->execute()->fetchField();
JOIN EXAMPLE:
---------------------------------
$query = db_select('uv', 'uv');
$query->leftjoin('gc', 'gct', 'gct.gcode_code = uv.voucher_code');
$result = $query->fields('uv')
->fields('gct', array('user_count'))
->condition('uv.expiry_date', time(), '>')
->condition('uv.g_id', $gid, '=')
->execute()
->fetchAll();
PDO SELECT
--------------------------------------
$result = db_select('dgm', 'dgm')
->fields('dgm', array('g_id'))
->condition('device_id', $device_id, '=')
->execute()
->fetchAll(PDO::FETCH_ASSOC);
------------------------------------