Very simple concept. I am selecting specific rows based on the date they where generated (X days ago) and organizing them by the time frame (in this case, the variable is OVERDUEDAYS). The only problem I am having now is defining OVERDUEDAYS and also selecting the entire rows content without having to define each column in the select statement.
Without further ado (this is what I am hoping to achieve):
SELECT DATEDIFF(CURDATE(), DATE(date_generated)) AS OVERDUEDAYS, * FROM invoices WHERE
(DATE(date_generated) = DATE_SUB(CURDATE(), INTERVAL 10 DAY)
OR
DATE(date_generated) = DATE_SUB(CURDATE(), INTERVAL 30 DAY)
OR
DATE(date_generated) = DATE_SUB(CURDATE(), INTERVAL 45 DAY)
OR
DATE(date_generated) = DATE_SUB(CURDATE(), INTERVAL 53 DAY)
OR
DATE(date_generated) = DATE_SUB(CURDATE(), INTERVAL 60 DAY))
AND
(paid=0 AND cancelled=0)
The query works great when you remove the wildcard and define each column manually... But that is a lot of columns and I would like to avoid that at all costs. Suggestions?
SELECT * FROM invoices WHERE colunm_id IN (
SELECT colunm_id FROM invoices WHERE
(DATE(date_generated) = DATE_SUB(CURDATE(), INTERVAL 10 DAY)
OR
DATE(date_generated) = DATE_SUB(CURDATE(), INTERVAL 30 DAY)
OR
DATE(date_generated) = DATE_SUB(CURDATE(), INTERVAL 45 DAY)
OR
DATE(date_generated) = DATE_SUB(CURDATE(), INTERVAL 53 DAY)
OR
DATE(date_generated) = DATE_SUB(CURDATE(), INTERVAL 60 DAY))
AND
(paid=0 AND cancelled=0)
)
Related
I need to do a single select on a table, but if Product='football' I need to do a couple of WHEREs, but if Product='something' then do something else. I am not sure if I need to do an IF statement, or a UNION or a CASE which I have never used.
Example of what I would like to have worked but obviously doesn't
SELECT *
FROM
orders
IF Product = 'Football'{
WHERE
AND RenewalDate < DATE_ADD(CURDATE(), INTERVAL 31 DAY)
AND RenewalDate > DATE_SUB(CURDATE(), INTERVAL 31 DAY)
}ELSE IF Product = 'Something'{
AND RenewalDate < DATE_ADD(CURDATE(), INTERVAL 10 DAY)
AND RenewalDate > DATE_SUB(CURDATE(), INTERVAL 10 DAY)
}
ORDER BY
RenewalDate
Now I know that looks like php but its just to show roughly what I want to happen
No if required. Just boolean logic:
SELECT o.*
FROM orders o
WHERE (Product = 'Football' AND
RenewalDate < DATE_ADD(CURDATE(), INTERVAL 31 DAY)
RenewalDate > DATE_SUB(CURDATE(), INTERVAL 31 DAY)
) OR
(Product = 'Something' AND
RenewalDate < DATE_ADD(CURDATE(), INTERVAL 10 DAY) AND
RenewalDate > DATE_SUB(CURDATE(), INTERVAL 10 DAY)
)
ORDER BY RenewalDate;
How can I get records in a table between NOW() and the previous 3am?
This would be easy if it's 9am, but how do I write this if it's 2am? i.e I want the trades between DATE_SUB(CURDATE(), INTERVAL 21 HOURS) and NOW(). I'm looking for some code which can do both without needing to check the time in usercode and choose between two sql statements.
I'm sure there's a simple solution to this, but it's eluding me.
A simple idea is to subtract three hours and compare the date:
where date(date_sub(col, interval 3 hour)) = (case when hour(date) >= 3 then curdate() else date_sub(curdate(), interval 1 day)
Or, more explicitly, just do the comparison in SQL:
where (hour(date) >= 3 and date(col) = curdate()) or
(hour(date) < 3 and date(col) = date_sub(curdate(), interval 1 day)
I'll add my own answer (I finally gave up and explored using IF and found it could be used inside SELECT statements), but #Gordon Linoff might be better - I've no idea which of these is faster.
SELECT IF (NOW() > DATE_ADD(CURDATE(), INTERVAL 3 HOUR),
DATE_ADD(CURDATE(), INTERVAL 3 HOUR),
DATE_SUB(CURDATE(), INTERVAL 21 HOUR))
which can then be used as the conditional on an outer SELECT.
I want to get data for the dates between 2015-05-01 and 2015-06-01 using SQL.
Please help me with the query.
The query I used is:
select *,count(id) as multiple_visitors
from table1
where id=123
and (date(server_time) between (CURDATE() - INTERVAL 31 DAY) AND CURDATE())
group by user_id having count(id)>1
You can do this with month() and year():
where month(server_time) = month(curdate() - interval 1 month) and
year(server_time) = year(curdate() - interval 1 month)
However, I recommend a slightly more complex expression:
where server_time >= date_sub(date_sub(curdate(), interval - day(curdate()) + 1 day), interval 1 month) and
server_time < date_sub(curdate(), interval - day(curdate()) + 1 day)
The advantage is that there are no functions on server_time, so the database engine can use an index, if appropriate.
As a note: the expression date_sub(curdate(), interval - day(curdate()) + 1 day) gets midnight on the first day of the month.
Try using "WHERE" with MONTH(date).
Like this:
SELECT * FROM Table
WHERE MONTH(date) = 1
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)
startTimestamp < date_sub(curdate(), interval 1 hour)
Will the (sub)query above return all records created within the hour? If not will someone please show me a correct one? The complete query may look as follows:
select * from table where startTimestamp < date_sub(curdate(), interval 1 hour);
Rather than CURDATE(), use NOW() and use >= rather than < since you want timestamps to be greater than the timestamp from one hour ago. CURDATE() returns only the date portion, where NOW() returns both date and time.
startTimestamp >= date_sub(NOW(), interval 1 hour)
For example, in my timezone it is 12:28
SELECT NOW(), date_sub(NOW(), interval 1 hour);
2011-09-13 12:28:53 2011-09-13 11:28:53
All together, what you need is:
select * from table where startTimestamp >= date_sub(NOW(), interval 1 hour);