I am reviewing a query which should pull in a list of transactions within a month, either based on its created_at date OR its returned_at date.
SELECT *
FROM (`transaction`)
WHERE `created_at` >= '2014-08-01'
AND `created_at` <= '2014-08-31'
OR `returned_at` >= '2014-08-01'
AND `returned_at` <= '2014-08-31'
My initial thought was that this would not work as intended since we do not have parentheses around the grouped WHERE logic. In other words, it would evaluate each condition one after another.
This is how I would have written the statement to ensure order of operations (I added parenthesis):
SELECT *
FROM (`transaction`)
WHERE
(`created_at` >= '2014-08-01' AND `created_at` <= '2014-08-31')
OR
(`returned_at` >= '2014-08-01' AND `returned_at` <= '2014-08-31')
Oddly enough, the initial query appears to be working though I am not sure if its because of my data set or not. Could someone clarify that both sql statements will always return the same results?