In my custom Drupal 8 plug-in I perform such query:
$select = db_select('my_table', 'e');
$select->addField('e', 'field1');
$select->condition('field3', 'NULL', '!=');
$select->condition('field4', array(11, 12, 31, 32, 33), 'IN');
$select->groupBy("e.field1");
$select->orderBy("e.field1");
$select->orderBy("e.field5");
$entries = $select->execute()->fetchAll(\PDO::FETCH_ASSOC);
And it works fine. It returns me all records grouped by field1
that meets the condition, orderered. But it returns only field1
column.
I wanted to get more columns, so I have added another $select->addField('e', 'field2');
just after first one:
$select = db_select('my_table', 'e');
$select->addField('e', 'field1');
$select->addField('e', 'field2');
$select->condition('field3', 'NULL', '!=');
$select->condition('field4', array(11, 12, 31, 32, 33), 'IN');
$select->groupBy("e.field1");
$select->orderBy("e.field1");
$select->orderBy("e.field5");
$entries = $select->execute()->fetchAll(\PDO::FETCH_ASSOC);
This time I get error:
Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42000]: Syntax error or access violation: 1055 Użyto 'my_database.e.field2' bez umieszczenia w group by:
Which means "my_database.e.field2
has been used without putting in into group by". But I don't want to group by every column I select.
Unfortunatelly Drupal 8 added ONLY_FULL_GROUP_BY
:(
Then, how can I use groupBy
and select more then one column at the same time?