Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5976921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:17:49+00:00 2026-05-22T21:17:49+00:00

I have a mysql query which works in a strange way. I am posting

  • 0

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:
enter image description here

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:
enter image description here
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:
enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:17:49+00:00Added an answer on May 22, 2026 at 9:17 pm

    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

    select '26/04/2011' between '13/05/11' AND '30/05/11';
    --
    T
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query which works correctly in MySQL. More background on it here
I have a query which works fine in MySQL, but when I run it
I have the following query which works fine with MySQL but refuses to work
I have a mysql query which outputs 6 columns of related data, I'm specifically
i have a MySQL SELECT query which fetches data from 6 tables using Mysql
I have the following query, which works in MySQL, and I need help&hints to
I have a MySQL query in which I want to include a list of
I have the following mysql query which I am running with php like so.
I have a complicated MySQL query which takes a lot of time, selecting from
I have a a php page which updates a mySql database it works fine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.