MySQL not showing value when using <= condition - mysql

i have records in my database like this
*just example
id
Name
created_at
1
Jono
2020-03-21 12:20:00
2
Abi
2020-03-25 12:20:00
3
Iko
2020-03-30 12:20:00
then i have simple query like this
SELECT * FROM `transaksi` WHERE `created_at` >= '2020-03-21' AND `created_at` <= '2020-03-30'
but that query only show id no 1 and 2 which is the date is < 2020-03-30
so my question is
why the third data which is still in range is not showing?
i have try something like this too
SELECT * FROM `transaksi` WHERE `created_at` >= DATE('2020-03-21') AND `created_at` <= DATE('2020-03-30')
but still have same issue
any sugestion?
thanks

This is the logical query you want here:
SELECT *
FROM transaksi
WHERE created_at >= '2020-03-21' AND created_at < '2020-04-01';
To explain what the above is actually saying, let's rewrite it using timestamp literals, with full H:M:S components:
SELECT *
FROM transaksi
WHERE created_at >= '2020-03-21 00:00:00' AND created_at < '2020-04-01 00:00:00';
Both of these queries will retain any record having a created date on or after midnight of 21-March-2020 and strictly before midnight of 1st-April-2020. The right side condition then includes the entire day of 31-March-2020.

You have to apply mysql DATE() function on created_at column to get date only without time.
SELECT *
FROM `transaksi`
WHERE DATE(`created_at`) >= '2020-03-21'
AND DATE(`created_at`) <= '2020-03-30'
SQL DEMO

Related

Search for a single date in a DATETIME or TIMESTAMP column

I have a problem retrieving data from a table between two dates.
Everything works as it should when two dates differ from each other, but when I want to search for all records from today, it searche nothing:
SELECT * from exampletable where created_date >= '2022-10-28' AND created_date<= '2022-10-28'
To get all records for a single day using a TIMESTAMP or DATETIME type column you have these options:
WHERE DATE(created_date) = '2022-10-28'
However - Wrapping a column into a function you will loose the ability to use an index.
Other ways which can use an index:
WHERE created_date BETWEEN '2022-10-28 00:00:00' AND '2022-10-28 23:59:59'
WHERE created_date >= '2022-10-28'
AND created_date < '2022-10-29'
WHERE created_date >= '2022-10-28'
AND created_date < '2022-10-28' + INTERVAL 1 DAY
Your query
where created_date >= '2022-10-28' AND created_date <= '2022-10-28'
is equivalent with
where created_date = '2022-10-28'
If created_date is of type DATETIME or TIMESTAMP it is the same as
where created_date = '2022-10-28 00:00:00'
and the query would only return records with the exact time of 00:00:00.

Fetch data between two date in mysql

This is my query
WHERE id = 14 AND start_time BETWEEN '2019-10-24 00:00:00' AND '2019-12-12 23:59:59'
ORDER BY created_date LIMIT 0 , 10
When I run this query then it returns me data of this data also. -> 2019-10-23T19:23:41.000Z
Issue: When I pass the 2019-10-24 then why it gives me data of 2019-10-23 date?
Note: start_time has a data type -> datetime in db.
It's not a issue by the way it's correct output.
Try this way
DATE_FORMAT(start_time, "%Y-%m-%d %H:%i:%s") as start_time
Due to diffrent formate, It may confused you.
use less than or equal to '<=' or greater than or equal to '>=' operator instead of BETWEEN.
Use this condition in your query.
WHERE id = 14 AND DATE(start_time) >= DATE('2019-10-24 00:00:00') AND DATE(start_time) <= DATE('2019-12-12 23:59:59') ORDER BY created_date LIMIT 0 , 10
OR
WHERE id = 14 AND DATE(start_time) >= '2019-10-24' AND DATE(start_time) <= '2019-12-12') ORDER BY created_date LIMIT 0 , 10

SQL Select rows comparing a datetime field that could be null

Let's guess we've got the following table:
- id (int)
- active (int)
- active_from (datetime) (NULL)
- active_until (datetime) (NULL)
Now, what I want to get is all the active records. An active record implies:
- active = 1
- active_from <= current_date (IF IT'S NOT NULL)
- active_until >= current_date (IF IT'S NOT NULL)
I'm looking for a query that applies these 3 requirements in one single query. I'm currently using:
SELECT * FROM product WHERE active = 1 AND active_from <= NOW() AND active_until >= NOW();
I will only get the behavior I want with rows that don't have NULL active_from or active_until.
Note: I know it would be more appropiate to compare the current date after storing it in a variable (posing it this way because I'm filling it with PHP parameters).
Thank you in advance.
SELECT * FROM YourTableName
WHERE active = 1
AND (active_from < CURDATE()
OR active_from IS NULL)
AND (active_until > CURDATE()
OR active_until IS NULL);
Use ifnull in your query (encase active_from and active_until in it):
https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#function_ifnull
So, basically, if the value is null, use another date instead. Which date is up to you and the specific business logic you need (ex: 2100-01-01 or 1900-01-01 etc.)
SELECT * FROM product
WHERE active = 1
AND (active_from < CURDATE()
AND active_from IS NOT NULL)
AND (active_until > CURDATE()
AND active_until IS NOT NULL)

WHERE returning too many rows for data NOT BETWEEN two dates

I want to return data that is not between current date and the last 7 days.
My SELECT statement appears ok, but it is also returning the current day's data.
SELECT
customer.id AS id,
customer.customer_id AS customer_id,
customer.name AS name,
customer.phone1 AS phone1,
customer.location_area AS location_area,
sales.post_date AS post_date
FROM
sales
INNER JOIN
customer
ON
sales.customer_id = customer.customer_id
WHERE
post_date
NOT BETWEEN
CAST( DATE_SUB(NOW(), INTERVAL 7 DAY) AS DATE )
AND
CAST( NOW() AS DATE )
ORDER BY
sales.id
DESC
LIMIT 30
Please note the customer_id field used in the ON clause is not a primary key in any of the two referenced tables.
What might be missing in my query?
This problem is usually confusion about the different meanings of DATE datatypes on the one hand and TIMESTAMP or DATETIME data types on the other.
Let's say NOW() is 1-April-2017 09:35. And, let's say you have a row in your sales table with a post_date value of 1-April-2017 08:20. Let's say your post_date column has the data type DATETIME.
Then your WHERE clause looks like this after values are applied.
WHERE '2017-04-01 08:20' NOT BETWEEN CAST( '2017-03-25 09:35' AS DATE )
AND CAST( '2017-04-01 09:35' AS DATE )
Applying the CAST operations, we get.
WHERE '2017-04-01 08:20' NOT BETWEEN '2017-03-25'
AND '2017-04-01'
Finally, when comparing a DATETIME or TIMESTAMP to a DATE value, the DATE value is interpreted as having a time of midnight. So your query looks like this:
WHERE '2017-04-01 08:20' NOT BETWEEN '2017-03-25 00:00:00'
AND '2017-04-01 00:00:00'
And, guess what? '2017-04-01 08:20' is after '2017-04-01 00:00:00'.
What you need is this:
WHERE
NOT (
post_date >= CURDATE() - INTERVAL 7 DAY --on or after midnight 2016-3-25
AND post_date < CURDATE() + INTERVAL 1 DAY --before midnight 2016-04-02
)
Please notice that this expression encompasses eight days total.
You can't use BETWEEN for this kind of comparison because you need < for the end of the range, and BETWEEN uses <= for the ends of all its ranges.
Also, CURDATE() is much easier to read than CAST(NOW() AS DATE).

MySQL - select data from database between two dates

I have saved the dates of a user's registration as a datetime, so that's for instance 2011-12-06 10:45:36. I have run this query and I expected this item - 2011-12-06 10:45:36 - will be selected:
SELECT `users`.* FROM `users` WHERE created_at >= '2011-12-01' AND
created_at <= '2011-12-06'
But is not. Exist any elegant way, how to select this item? As a first idea that I got was like 2011-12-06 + 1, but this doesn't looks very nice.
Your problem is that the short version of dates uses midnight as the default. So your query is actually:
SELECT users.* FROM users
WHERE created_at >= '2011-12-01 00:00:00'
AND created_at <= '2011-12-06 00:00:00'
This is why you aren't seeing the record for 10:45.
Change it to:
SELECT users.* FROM users
WHERE created_at >= '2011-12-01'
AND created_at <= '2011-12-07'
You can also use:
SELECT users.* from users
WHERE created_at >= '2011-12-01'
AND created_at <= date_add('2011-12-01', INTERVAL 7 DAY)
Which will select all users in the same interval you are looking for.
You might also find the BETWEEN operator more readable:
SELECT users.* from users
WHERE created_at BETWEEN('2011-12-01', date_add('2011-12-01', INTERVAL 7 DAY));
SELECT users.* FROM users WHERE created_at BETWEEN '2011-12-01' AND '2011-12-07';
You need to use '2011-12-07' as the end point as a date without a time default to time 00:00:00.
So what you have actually written is interpreted as:
SELECT users.*
FROM users
WHERE created_at >= '2011-12-01 00:00:00'
AND created_at <= '2011-12-06 00:00:00'
And your time stamp is: 2011-12-06 10:45:36 which is not between those points.
Change this too:
SELECT users.*
FROM users
WHERE created_at >= '2011-12-01' -- Implied 00:00:00
AND created_at < '2011-12-07' -- Implied 00:00:00 and smaller than
-- thus any time on 06
Another alternative is to use DATE() function on the left hand operand as shown below
SELECT users.* FROM users WHERE DATE(created_at) BETWEEN '2011-12-01' AND '2011-12-06'
Have you tried before and after rather than >= and <=? Also, is this a date or a timestamp?
Searching for created_at <= '2011-12-06' will search for any records that where created at or before midnight on 2011-12-06
. You want to search for created_at < '2011-12-07'.
Maybe use in between better. It worked for me to get range then filter it
You can use MySQL DATE function like below
For instance, if you want results between 2017-09-05 till 2017-09-09
SELECT DATE(timestamp_field) as date FROM stocks_annc WHERE DATE(timestamp_field) >= '2017-09-05' AND DATE(timestamp_field) <= '2017-09-09'
Make sure to wrap the dates within single quotation ''
Edit:
A better solution would be this. It would make sure that it uses the index if any exists.
select date(timestamp_field) as date from stocks_annc where time_stamp_field >= '2022-01-01 00:00:00' and time_stamp_field <= '2022-01-10 00:00:00'
Hope this helps.