MySQLism: avoid ambiguous queries

Last updated on
8 September 2016

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

See bug: http://drupal.org/node/1044410

MySQL will accept queries like:
SELECT n.title, n.nid, n.type, s.reminder_email, s.forwarding_email, e.event_start AS event_start, e.timezone FROM node n INNER JOIN signup s ON s.nid = n.nid INNER JOIN event e ON n.nid = e.nid INNER JOIN event_timezones tz ON e.timezone = tz.timezone WHERE (s.send_reminder = 1) AND (n.type = 'event') AND (('2011-01-29 09:05:03' > ((e.event_start - CASE WHEN tz.is_dst = 1 THEN tz.offset_dst ELSE tz."offset" END) - INTERVAL 's.reminder_days_before days'))) AND (('2011-01-29 09:05:03' <= ((e.event_start - CASE WHEN tz.is_dst = 1 THEN tz.offset_dst ELSE tz."offset" END))))

Notice the MySQL syntax:
' s.reminder_days_before days'

In SQL 99, the correct syntax would be '1 days', '2days', etc ..
The interval is only a fixed value. It cannot be a variable column.

Therefore, if s.reminder_days_before has several values,
it is likely that MySQL will accept the query and run the query using one of the values.
This can cause random bugs, very hard to find.

PostgreSQL does not accept ambiguous queries.
To prevent bugs, PostgreSQL will throw an error.

And you should rewrite your query to be non-ambiguous.

Help improve this page

Page status: No known problems

You can: