function dbtng_example_execute_group_by_select_query in Examples for Developers 7
The code below will result in the following query SELECT ex.pid AS pid, ex.uid AS uid, ex.name AS name, ex.surname AS surname, ex.age AS age FROM {dbtng_example} ex GROUP BY ex.age
Related topics
1 call to dbtng_example_execute_group_by_select_query()
- dbtng_example_grouping_list in dbtng_example/
dbtng_example.module  - This function groups the result set by the specified field and render a list of entries in the database
 
File
- dbtng_example/
dbtng_example.module, line 642  - This is an example outlining how a module can make use of the new DBTNG database API in Drupal 7.
 
Code
function dbtng_example_execute_group_by_select_query() {
  $select = db_select('dbtng_example', 'ex');
  // Select these specific fields for the output.
  $select
    ->fields('ex', array(
    'name',
  ));
  // Count('name') how many times same name comes in table .
  $select
    ->addExpression('COUNT(ex.name)', 'count');
  // 'n.name' is used for groupBy clause.
  $select
    ->groupBy("ex.name");
  $output = $select
    ->execute()
    ->fetchAll();
  return $output;
}