Under MySQL 5.7 (or higher), a Drupal site sometimes shows errors similar to these:
Uncaught PHP Exception Drupal\Core\Database\DatabaseExceptionWrapper: "SQLSTATE[HY000]: General error: 3065 Expression [... redacted ...] of ORDER BY clause is not in SELECT list, references column '[... redacted ...]' which is not in SELECT list; this is incompatible with DISTINCT: [...]
There are 2 options:
For the second option, you can modify your site's settings.php file (around the Acquia "require" line) to something similar to the snippet below.
Important: We can only recommend this for MySQL 5.7. This will cause a fatal error with MySQL 8 (see the note below).
NOTES:
- This code is provided as-is, and could potentially have side effects; we recommend testing this thoroughly before deployment.
- Also, we recommend re-testing this code on any updates or changes to Drupal core and/or the Database backend, especially with upgrades to MySQL 8.0.
- If you need this code still and your MySQL version is 8.0, try removing ,NO_AUTO_CREATE_USER as it no longer is a part of MySQL after that.
/**
* NOTE: EDIT and test this snippet; review all sections with [EDIT-ME]
*/
if (file_exists('/var/www/site-php/[EDIT-ME]')) {
/**
* Overriden init_commands of $databases on acquia.
* See https://support.acquia.com/hc/en-us/articles/360005253954-Fixing-database-deadlocks
*/
global $conf, $databases;
$conf['acquia_hosting_settings_autoconnect'] = FALSE;
require '/var/www/site-php/[EDIT-ME]/[EDIT-ME]-settings.inc';
/**
* Fix for MySQL 5.7 related DISTINCT and ORDER BY mysql query
* Override setting of core /docroot/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
* Check again when upgrading drupal core
* Follow Drupal.org issue https://www.drupal.org/project/drupal/issues/2856270
* The following avoids setting the "ANSI" option for sql_mode.
*/
$databases['default']['default']['init_commands'] = [
'sql_mode' => "SET sql_mode = 'STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER'"
];
acquia_hosting_db_choose_active();
}
Site Factory users will need to split this code between pre-settings.php and the post-settings.php.
Drupal 7:
$conf['acquia_hosting_settings_autoconnect'] = FALSE;
Drupal 8:
global $conf, $databases;
$conf['acquia_hosting_settings_autoconnect'] = FALSE;
Drupal 7 and Drupal 8:
Again, if you need this code still and your MySQL version is 8.0, try removing ,NO_AUTO_CREATE_USER as it no longer is a part of MySQL after that.
$databases['default']['default']['init_commands'] = [
'sql_mode' => "SET sql_mode = 'STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER'"
];
acquia_hosting_db_choose_active();
Drupal in conjunction with newer MySQL versions have adopted certain defaults which can cause this error to be thrown when SELECT queries do not include fields that are then included in ORDER BY clauses. Taking either of the above steps should resolve the issue. The strongest option is the first one, that is, editing your code to remove the need for the settings.php workaround.
If this content did not answer your questions, try searching or contacting our support team for further assistance.
Wed Oct 22 2025 08:59:29 GMT+0000 (Coordinated Universal Time)