Select date that's in given date's interval - mysql

I have a table of dates, and want to select dates that are in 1,5 hour interval of given date.
For example:
-- '2013-06-11 18:40' is the given date, and I want all dates that are
-- inside +/- 1.5 hour interval
SELECT datum
FROM table
WHERE date(datum) > date( date_sub( '2013-06-11 18:40', interval 1.5 hour ) )
AND date(datum) < date( date_add( '2013-06-11 18:40', interval 1.5 hour ) )

The MySQL date function takes the date part of a datetime. That's not advisable when you're looking for a 3 hour interval :)
Try:
WHERE datum between date_sub( '2013-06-11 18:40', interval 90 minute) and
date_add( '2013-06-11 18:40', interval 90 minute)

I used integer minutes instead of hours plus removed DATE function as Andomar suggested:
SELECT datum
FROM table
WHERE
datum > date( date_sub( '2013-06-11 18:40', interval 90 MINUTE ) )
AND
datum < date( date_add( '2013-06-11 18:40', interval 90 MINUTE ) )

Related

SQL turnover of the day before last year

I'm blocking on an SQL query. I am looking to recover the turnover of last year but the day before. For example: we are the 13/08/19 and I wish the turnover of yesterday of last year so 12/08/18.
How can I do that? Here is my request:
SELECT SUM(total_paid/1.2)
FROM '._DB_PREFIX_.'orders
WHERE o.date_add BETWEEN DATE_FORMAT(
CASE
WHEN YEAR( DATE_SUB( now(), INTERVAL 364 DAY ) ) = YEAR(now() )
THEN DATE_SUB(concat(YEAR(now()),'-',MONTH(now()),'-01'), INTERVAL 371 DAY)
ELSE DATE_SUB(concat(YEAR(now()),'-',MONTH(now()),'-01'), INTERVAL 364 DAY) END, '%Y/%m/%d') AND
CASE
WHEN YEAR( DATE_SUB( now(), INTERVAL 364 DAY ) ) = YEAR(now() )
THEN DATE_SUB(now(), INTERVAL 371 DAY)
ELSE DATE_SUB(now(), INTERVAL 364 DAY) END
AND valid=1
table
Thanks for help.
It seems you should be able to simplify your query to something like this:
SELECT SUM(total_paid/1.2)
FROM '._DB_PREFIX_.'orders
WHERE DATE(o.date_add) = CURDATE() - INTERVAL 1 YEAR - INTERVAL 1 DAY
Note that dependent on how you want to treat February 29, you may want to change the expression to
CURDATE() - INTERVAL 1 DAY - INTERVAL 1 YEAR
In the first form, 2020-02-29 will map to 2019-02-27, in the second it will map to 2019-02-28.
{
select sum(your_total_column/1.2) from your_tab
where now() =(CASE WHEN mod(year(your_date_column),4) = 0
THEN DATE_ADD(your_date_column,INTERVAL 367 DAY)
ELSE DATE_ADD(your_date_column,INTERVAL 366 DAY));}
The idea is find if your_date_column is leap year if it is leap year then add 367 else add 366 and compare with current date.

Improvement in the below mysql query

SELECT * FROM *****
WHERE ((first_appeared_on BETWEEN DATE_SUB( '2015-07-28' , INTERVAL 1 DAY ) AND '2015-07-28')
or (last_appeared_on BETWEEN DATE_SUB( '2015-07-28' , INTERVAL 1 DAY ) AND '2015-07-28'))
In the above query I don't want to type the date (2015-07-28) again and again Can I use a variable and assign that variable a value
day = '2015-07-28';
SELECT * FROM *****
WHERE ((first_appeared_on BETWEEN DATE_SUB(day , INTERVAL 1 DAY ) AND day)
or (last_appeared_on BETWEEN DATE_SUB( day , INTERVAL 1 DAY ) AND day))
This can be done in MySQL with a user-defined variable:
SET #day = '2015-07-28';
SELECT * FROM *****
WHERE ((first_appeared_on BETWEEN DATE_SUB( #day , INTERVAL 1 DAY ) AND #day)
or (last_appeared_on BETWEEN DATE_SUB( #day , INTERVAL 1 DAY ) AND #day));

SELECT specific date with JOIN

Table Client contains last_name and email column while table Product contain email and expiry_date column with 13 row of records. This syntax correctly select only 2 rows from Product table.
SELECT * FROM `Product` WHERE expiry_date > DATE_ADD( CURRENT_DATE, INTERVAL 24 DAY )
AND expiry_date < DATE_ADD( CURRENT_DATE, INTERVAL 24 + 1 DAY )
Now I want to join it with Client table to select last_name with matching email. Unfortunately my JOIN syntax below select all 13 records from Product table.
SELECT * FROM `Product` JOIN `Client`
ON product.email=client.email
AND (expiry_date > DATE_ADD( CURRENT_DATE, INTERVAL 24 DAY )
AND expiry_date < DATE_ADD( CURRENT_DATE, INTERVAL 24 + 1 DAY ))
This syntax also does not work. All 13 rows from Product table was selected:
SELECT * FROM
(
SELECT * FROM `Product` WHERE expiry_date > DATE_ADD( CURRENT_DATE, INTERVAL 24 DAY )
AND expiry_date < DATE_ADD( CURRENT_DATE, INTERVAL 24 +1 DAY )
) A
LEFT JOIN Client ON A.email=client.email
WHERE A.expiry_date > DATE_ADD( CURRENT_DATE, INTERVAL 24 DAY )
AND A.expiry_date < DATE_ADD( CURRENT_DATE, INTERVAL 24 +1 DAY )
How do I select only 2 last_name and not all 13. I hope I explained it clearly. Thanks in advance.
The way you are specifying your JOIN conditions, everything matches. You should specify a WHERE clause instead
SELECT * FROM `Product` JOIN `Client`
ON product.email=client.email
WHERE expiry_date > DATE_ADD( CURRENT_DATE, INTERVAL 24 DAY )
AND expiry_date < DATE_ADD( CURRENT_DATE, INTERVAL 24 + 1 DAY )
You need to use GROUP BY to get only 2 rows instead of all rows in Product table. For example:
SELECT * FROM `Product` JOIN `Client`
ON product.email=client.email
AND (expiry_date > DATE_ADD( CURRENT_DATE, INTERVAL 24 DAY )
AND expiry_date < DATE_ADD( CURRENT_DATE, INTERVAL 24 + 1 DAY ))
GROUP BY expiry_date

MySql Date Query Incorrect result

When i run the below query, it returns all the results for dates falling within Date_add (CURDATE() AND CURDATE(), interval 30 day) but does not include results for Date_sub (CURDATE() AND CURDATE(), interval 15 day)
I know the data exists when I query with exact clause of deadline = '2015-01-15'
What could be wrong?
SELECT bug_id,
bug_status,
resolution,
short_desc,
deadline
FROM bugs
WHERE bug_status IN ( 'RESOLVED' )
AND deadline BETWEEN Curdate() AND Date_add(Curdate(), interval 30 day)
OR deadline BETWEEN Curdate() AND Date_sub(Curdate(), interval 15 day)
The rand for between is ordered. So, the second between is not correct. In addition, you probably want parentheses:
WHERE bug_status IN ( 'RESOLVED' ) AND
(deadline BETWEEN Curdate() AND Date_add(Curdate(), interval 30 day) OR
deadline BETWEEN Date_sub(Curdate(), interval 15 day) AND Curdate()
)
I mean, you might not want parentheses, so the query would then be:
WHERE (bug_status IN ( 'RESOLVED' ) AND
deadline BETWEEN Curdate() AND Date_add(Curdate(), interval 30 day)
) OR
deadline BETWEEN Date_sub(Curdate(), interval 15 day) AND Curdate()
In this case, the parentheses are redundant but they clarify the logic.
between's arguments should always be value BETWEEN low AND high. If you flip low and high, it'll return false. Moreover, you can unify both conditions to one:
deadline BETWEEN Date_sub(Curdate(), interval 15 day) AND
Date_add(Curdate(), interval 30 day)

In MySQL, how do I do a date query?

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 )