Last updated November 23, 2012. Created by drupalshrek on November 23, 2012.
Log in to edit this page.

Grouping

To group by a given field, use the groupBy() method.

<?php
$query
->groupBy('uid');
?>

The above code will instruct the query to group by the uid field. Note that the field name here should be the alias created by the addField() or addExpression() methods, so in most cases you will want to use the return value from those methods here to ensure the correct alias is used. To group by multiple fields, simply call groupBy() multiple times in the order desired.

About this page

Drupal version
Drupal 7.x
Audience
Programmers
Level
Intermediate, Advanced
Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.