I have a mysql query which works in a strange way. I am posting the 2 queries with input data changed and the output are listed under each query.
Query 1 (Area to be noted BETWEEN '13/05/11' AND '30/05/11'):
SELECT COUNT(pos_transaction_id) AS total, DATE_FORMAT(pt.timestamp,'%d-%m-%Y %H:%i:%S') AS Date, SUM(amount) AS amount FROM pos_transactions pt WHERE DATE_FORMAT(pt.timestamp,'%e/%m/%y') BETWEEN '13/05/11' AND '30/05/11' GROUP BY WEEK(pt.timestamp) ORDER BY pt.timestamp
Output:
Query 2 (Area to be noted BETWEEN '3/05/11' AND '30/05/11'):
SELECT COUNT(pos_transaction_id) AS total, DATE_FORMAT(pt.timestamp,'%d-%m-%Y %H:%i:%S') AS Date, SUM(amount) AS amount FROM pos_transactions pt WHERE DATE_FORMAT(pt.timestamp,'%e/%m/%y') BETWEEN '3/05/11' AND '30/05/11' GROUP BY WEEK(pt.timestamp) ORDER BY pt.timestamp
Output:

Now when the range is increased in the second query why am I getting just one record ? And even in the first query I am getting records which is out of range. What is wrong with it??
EDIT
The changed query looks like this and still not doing what I wanted it to do.
SELECT COUNT(pos_transaction_id) AS total,
DATE_FORMAT(pt.timestamp,'%d-%m-%Y %H:%i:%S') AS Date,
SUM(amount) AS amount
FROM pos_transactions pt
WHERE DATE_FORMAT(pt.timestamp,'%e/%m/%y') BETWEEN STR_TO_DATE('01/05/11','%e/%m/%y') AND STR_TO_DATE('30/05/11','%e/%m/%y')
GROUP BY WEEK(pt.timestamp) ORDER BY pt.timestamp
The output is:

I think you’re seeing the result of the intersection of two bad practices.
First, the date_format() function returns a string. Your WHERE clause does a string comparison. In PostgreSQL
That’s because the string ’26’ is between the strings ’13’ and ’30’. If you write them as dates, though, PostgreSQL will correctly tell you that ‘2011-04-26’ (following the datestyle setting on my server) isn’t in that range.
Second, I’m guessing that the odd out-of-range values appear because you’re using an indeterminate expression in your aggregate. The expression
WEEK(pt.timestamp)doesn’t appear in the SELECT list. I think every other SQL engine on the market will throw an error if you try to do that. Since it’s not in the SELECT list, MySQL will return an apparently random value from that aggregate range.To avoid these kinds of errors, don’t do string comparisons on date or timestamp ranges, and don’t use indeterminate aggregate expressions.
Posting DDL and minimal SQL INSERT statements to reproduce the problem helps people help you.