I'm running SQL queries with GROUP BY
clauses using WordPress's $wpdb
global object, like this:
$wpdb->get_results("SELECT * FROM foobar GROUP BY foo, bar");
I'm discovering that $wpdb
will silently accept bad GROUP BY clauses that would otherwise throw an error. When I run the queries by hand in the MySQL prompt, I get an error that looks like this:
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'foo.bar.foobar' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
It turns out that WordPress disables sql_mode
only_full_group_by
.
How do I temporarily change sql_mode
to enable only_full_group_by
? I would like to do this temporarily, so that code in other WordPress plugins is not broken.