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;
Related
get the details in between 365 to 500 days, from today
select * from YOURTABLE WHERE DATE_ADD(CURDATE(), INTERVAL -365 DAY) < CURDATE() < DATE_ADD(CURDATE(), INTERVAL 500 DAY)
How to get the rows out of a MySQL DB where the field date is before NOW + 2 weeks?
I have tried
WHERE date_ready < DATE_SUB(CURDATE(), INTERVAL 2 WEEK)
But that is not the rows returning that I expect.
Or even, now() minus 2 week,
where date_ready < (NOW() - INTERVAL 2 WEEK)
with just date
where date_ready < (CURDATE() - INTERVAL 2 WEEK)
You're querying dates that are before today minus two weeks, not plus. You should use date_add instead of date_sub:
WHERE date_ready < DATE_ADD(CURDATE(), INTERVAL 2 WEEK)
-- Here -----------^
try this:
WHERE date_ready < DATE_ADD(now(), INTERVAL 2 WEEK)
-- Here -----------^
and
WHERE date_ready < DATE_ADD(CURDATE(), INTERVAL + 14 Day)
-- Here -----------^
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)
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)
)
Is it possible to use a function or a variable as the label for a column in MySQL?
So for example if you wanted to include the date, something like:
select
sum(P.AMOUNT) AS monthname(date_sub(CURDATE(), INTERVAL 1 MONTH))
from
PAYMENT P
WHERE
P.CREATED_DATE > (date_sub(CURDATE(), INTERVAL 1 MONTH));
This fails, as it's not the correct syntax for an AS alias. I've also tried this:
SET #s = monthname(date_sub(CURDATE(), INTERVAL 1 MONTH));
select
sum(P.AMOUNT) AS #s
from
PAYMENT P
WHERE
P.CREATED_DATE > (date_sub(CURDATE(), INTERVAL 1 MONTH));
Which also doesn't work...
Any ideas?
EDIT - With the above example i realize you could actually just use an additional column to return the date info. The problem is i have a few columns with various date ranges, like this:
select
sum(IF((P.CREATED_DATE BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 MONTH) and DATE_SUB(CURDATE(), INTERVAL 0 MONTH)),P.amount,0)) AS 'Month 0',
sum(IF((P.CREATED_DATE BETWEEN DATE_SUB(CURDATE(), INTERVAL 2 MONTH) and DATE_SUB(CURDATE(), INTERVAL 1 MONTH)),P.amount,0)) AS 'Month -1',
sum(IF((P.CREATED_DATE BETWEEN DATE_SUB(CURDATE(), INTERVAL 3 MONTH) and DATE_SUB(CURDATE(), INTERVAL 2 MONTH)),P.amount,0)) AS 'Month -2'
from
PAYMENT P
WHERE
P.AMOUNT < 100000;
So it's the "Month 0" etc i'm looking to replace with a function/query.