Basically, I've got a query which looks like the following
SELECT
*
FROM
`transactions`
WHERE
`date` > DATE_SUB(NOW(), INTERVAL 1 DAY)
AND
`status` = '1'
How can I do it so I can get it from just today, not the past 24 rolling hours; the same for a week & month etc..?
Use DATE() to get the date of your date column and compare it to today's date using CURDATE()
WHERE
DATE(`date`) = CURDATE()
Use
SELECT *
FROM `transactions`
WHERE `date` >= curdate() AND `date` < curdate() + interval 1 day
AND `status` = '1'
which can make use of indexes to speed up the query
Related
I have a table with multiple datetime columns in a mySQL 5.6 database.
email_id
email_sent_date (datetime)
email_replied_date (datetime)
email_bounced_date(datetime)
email_archived (datetime)
I want to retrieve all emails where the latest datetime of the group (email_sent_date,email_replied_date, email_bounced_date, email_archived) is at least 1 month ago from today.
I would like to do something like this, even though i think I can't use this max(), but you'll get a sense of what I am trying to achieve:
SELECT *
FROM table
WHERE MAX(email_sent_date,email_replied_date, email_bounced_date, email_archived) <= CURDATE() - INTERVAL 30 DAY
How to achieve this ?
You want the function greatest(), not max():
SELECT *
FROM table
WHERE GREATEST(email_sent_date, email_replied_date, email_bounced_date, email_archive) <= CURDATE() - INTERVAL 30 DAY;
Note: If any of the values are NULL, then the row will be filtered out.
I prefer explicit comparisons because I think the logic is easier to follow:
SELECT *
FROM table
WHERE email_sent_date <= CURDATE() - INTERVAL 30 DAY AND
email_replied_date <= CURDATE() - INTERVAL 30 DAY AND
email_bounced_date <= CURDATE() - INTERVAL 30 DAY AND
email_archive <= CURDATE() - INTERVAL 30 DAY;
EDIT:
Handling NULL values requires explicit checks. Perhaps:
SELECT *
FROM table
WHERE (email_sent_date <= CURDATE() - INTERVAL 30 DAY OR email_sent_date IS NULL) AND
(email_replied_date <= CURDATE() - INTERVAL 30 DAY OR email_replied_date IS NULL) AND
(email_bounced_date <= CURDATE() - INTERVAL 30 DAY OR email_bounced_date IS NULL) AND
(email_archive <= CURDATE() - INTERVAL 30 DAY OR email_archive IS NULL);
I want to select users whose someDateTime field is either NULL, or older than 3 days ago.
SELECT `id`
FROM `users`
WHERE `someDateTime` IS NULL
OR `someDateTime` < NOW() - INTERVAL 3 DAYS
LIMIT 1
I receive a syntax error with this query.
I also tried DATE_SUB(NOW(), INTERVAL 3 DAYS) but that fails too.
I think you miss the bracket and
use DAY only instead of DAYS.
and more use CURDATE() instead of NOW(). and use = to get data of today date.
CURDATE() returns the DATE part of the current time. Manual on CURDATE()
NOW() returns the date and time portions as a timestamp in various formats, depending on how it was requested. Manual on NOW().
SELECT `id`
FROM `users`
WHERE `someDateTime` IS NULL
OR `someDateTime` <=
( CURDATE() - INTERVAL 3 DAY )
Use this:
SELECT DATE_ADD(Date(NOW()), INTERVAL 3 DAY)
or
SELECT DATE_ADD(Date(NOW()), INTERVAL -3 DAY)
Then subtract your DB date or any....
I want to select all dates that are between the current date and 3 months before.
I tried using this query but it isn't working right.
$sql = mysql_query("
SELECT *
FROM date
WHERE d_date BETWEEN NOW() AND NOW() - INTERVAL 3 MONTH
");
Please if you could help me write the right syntax.
You need to swap your bounaries, and it will work:
SELECT * FROM date
WHERE d_date BETWEEN now() - INTERVAL 3 MONTH AND now()
For example, this query returns true (SQLFiddle):
SELECT (now() - interval 1 month)
BETWEEN now() - interval 3 month AND now()
SELECT * FROM Table
WHERE anydate_col BETWEEN NOW() AND DATE_ADD( NOW() , INTERVAL +3 MONTH)
I want to do SELECT count(*) FROM users where created_at within 3 days ago
Created_at is datetime column.
Use for a date three days ago:
WHERE t.date >= DATE_ADD(CURDATE(), INTERVAL -3 DAY);
Check the DATE_ADD documentation.
Or you can use:
WHERE t.date >= ( CURDATE() - INTERVAL 3 DAY )
In MYSQL I have a '0' inserted into a DATETIME field called DT.
making it a "0000-00-00 00:00:00".
I need to compare DT to NOW() and tell if the result is in the past or not.
Some dates have real values, and some dates have a '0' (as said get the "0000-00-00 00:00:00").
How can I check that column DT + INTERVAL 3 MONTH < NOW() ?
Thanks!
It seems like you just want to ignore the 0 dates, so you can simply filter out the 0 dates in your WHERE clause like this:
SELECT * FROM mytable
WHERE DT != '0000-00-00 00:00:00'
AND DATE_ADD(DT, INTERVAL 3 MONTH) < NOW()
Here's the optimized version of that query. You should try to put your functions on the right side of the criteria so that MySQL can utilize indexes:
SELECT * FROM mytable
WHERE DT != '0000-00-00 00:00:00'
AND DT < DATE_ADD(NOW(), INTERVAL -3 MONTH)
You can use the DATE_ADD() function to get what you are looking for:
SELECT * FROM mytable WHERE DATE_ADD(`DT`, INTERVAL 3 MONTH) < NOW();
This will return all columns where (DT + INTERVAL 3 MONTH) < NOW()