I have this query string here ($month = 11):
select * from members where CURDATE() = DATE_ADD(renewal_date, INTERVAL $month MONTH);
What this query string will do is get all my members with a renewal_date from 11 months ago from the current date
I also have two other fields, reminder_date and blocker. What I am looking to add is, to get members with a reminder_date from 1 month ago from the current date and if the blocker is not equal to 1 (blocker can be 0 or 1)
How would I do that?
Thanks,
J
You can try this:
SELECT *
FROM members
WHERE CURDATE() = DATE_ADD(renewal_date, INTERVAL $MONTH MONTH)
OR (
CURDATE() = DATE_ADD(reminder_date, INTERVAL 1 MONTH)
AND blocker = 0
);
This will get your original results and add the ones with reminder_date of one month ago and blocker = 0
Related
I want to count the distinct number of fd_id over the time between today and yesterday, between today and 3 days ago, between today and 5 days ago, between today and 7 days ago, between today and 15 days ago, between today and 30 days ago.
My data table looks like the following:
user_id. fd_id. date
1. 123a. 20201010
1. 123a. 20201011
1. 124a. 20201011
...
and the desired result is of the following format:
user_id count_fd_id_1d count_fd_id_3d ... count_fd_id_30d
Specifically, I know I can do the following 6 times and join them together (some column bind method):
select user_id, count(distinct fd_id) as count_fd_id_1d
from table
where date <= today and date >= today-1 (#change this part for different dates)
select user_id, count(distinct fd_id) as count_fd_id_3d
from table
where date <= today and date >= today-3 (#change this part for different dates)
...
I am wondering how I may do this in one shot without running almost identical code for 6 times.
You can use conditional aggregation:
select user_id,
count(distinct case when date >= current_date - 1 day and date < current_date then fd_id end) as cnt_1d,
count(distinct case when date >= current_date - 3 day and date < current_date then fd_id end) as cnt_3d,
...
from mytable
goup by user_id
You can play around with the date expressions to set the ranges you want. The above works on entire days, and does not include the current day.
If the date column in the the table really does look like that (not in date/datetime format), I think you need to use STR_TO_DATE() to convert it to date format then uses DATEDIFF to check the date differences. Consider this example query:
SELECT user_id,
MAX(CASE WHEN ddiff=1 THEN cn END) AS count_fd_id_1d,
MAX(CASE WHEN ddiff=2 THEN cn END) AS count_fd_id_2d,
MAX(CASE WHEN ddiff=3 THEN cn END) AS count_fd_id_3d,
MAX(CASE WHEN ddiff=4 THEN cn END) AS count_fd_id_4d,
MAX(CASE WHEN ddiff=5 THEN cn END) AS count_fd_id_5d
FROM (SELECT user_id,
DATEDIFF(CURDATE(), STR_TO_DATE(DATE,'%Y%m%d')) ddiff,
COUNT(DISTINCT fd_id) cn
FROM mytable
GROUP BY user_id, ddiff) A
GROUP BY user_id;
At the moment, if you check date value simply by using direct subtraction, you'll get incorrect result. For example:
*your current date value - how many days:
'20201220' - 30 = '20201190' <-- this is not correct.
*if you convert the date value and using the same subtraction:
STR_TO_DATE('20201220','%Y%m%d') - 30 = '20201190' <-- still get incorrect.
*convert date value then uses INTERVAL for the date subtraction:
STR_TO_DATE('20201220','%Y%m%d') - INTERVAL 30 DAY = '2020-11-20'
OR
DATE_SUB(STR_TO_DATE('20201220','%Y%m%d'),INTERVAL 30 DAY) = '2020-11-20'
*IF your date column is storing standard date format value, then omit STR_TO_DATE
'2020-12-20' - INTERVAL 30 DAY = '2020-11-20'
OR
DATE_SUB('2020-12-20',INTERVAL 30 DAY) = '2020-11-20'
Check out more date manipulation in MySQL.
For the question, I made a fiddle with a bunch of testing.
I am not having much knowledge on mysql queries.. and i'm trying to get values from database from last two hours from now ..I have searched about it and i have found some related posts too.. but unfortunately i am unable to implement the logic.. please help me solve this issue..
here is the mysql query
select *
from `ordermaster`
where ((
`ordermaster`.`Pick_date`
= curdate())
and (`ordermaster`.`Pick_time`
<= (now() - interval 2 hour))
and (`ordermaster`.`Status`
= 2))
were, Pick_Date = "2017-04-19" (today date) and Pick_Time = "10:00:00" (24 hours format)
Try the Following:
Select * From ordermaster om
Where Concat(om.Pick_date,' ', om.Pick_time) as date Between
(now() - interval 2 hour) and now()
AND Status = 2
You can use the between keyword
select *
from `ordermaster`
where `Pick_date` = curdate() and
`Pick_time` between (now() - interval 2 hour) and now() and
`Status` = 2
Edit
Based on our chat, where we found out the GoDaddy server you have your db hosted on is 12.5 hours ahead of your local time, the final query you should use is
select *
from `ordermaster`
where `Pick_date` = curdate() and
`Pick_time` <= now() + interval 10 hour + interval 30 minute and
`Status` = 2
I am trying to get the amount of data for the last 30 days.
SELECT ( Now() - interval 1 month ),
Count(flightid) AS count
FROM flight
WHERE flightstatus = 0
AND flightvisibility = 1
AND flightvaliddate > Now()
AND flightvaliddate >= ( Now() - interval 1 month )
Right now this is working ok and it's giving me only 1 row that corresponds to the same day of last month.
What I would like is to get the remaining data from each day until now. How can I do this?
I am using MySQL.
The condition in the WHERE clause is wrong.
And since you want day wise data of last thirty days till now then you must have to use GROUP BY.
SELECT
DATE(flightvalidate) AS flightValidateDate,
Count(flightid) AS count
FROM
flight
WHERE
flightstatus = 0
AND flightvisibility = 1
AND DATE(flightvaliddate) >= CURDATE() - INTERVAL 1 MONTH
GROUP BY flightValidateDate
ORDER BY flightvalidate
I am trying to get the total amount of registered users per day. At the moment I am using this:
$sql = "SELECT name, email FROM users WHERE DATE_SUB(NOW(), INTERVAL 1 DAY) < lastModified"
But I am not sure if this works per day or per 24 hours?
For example, a user who registered 22 hours ago shouldn't be returned. I just want the user of today(=Tuesday).
lastModified is, presumably, a datetime. To convert this into a date you can simply wrap it in DATE() i.e. DATE(lastModified). DATE() returns the date part of a datetime value which is effectively 00:00 on that day.
SELECT
name,
email
FROM users
WHERE DATE(lastModified) = DATE( DATE_SUB( NOW() , INTERVAL 1 DAY ) )
Using this to match a WHERE though would be inefficient as all rows would require DATE applied to them and so it would probably scan the whole table. It is more efficient to compare lastModified to the upper and lower bounds you are looking for, in this case >= 00:00 on SUBDATE(NOW(),INTERVAL 1 DAY) and < 00:00 on NOW()
Therefore you can use BETWEEN to make your select giving the following.
SELECT
name,
email
FROM users
WHERE lastModified
BETWEEN DATE( DATE_SUB( NOW() , INTERVAL 1 DAY ) )
AND DATE ( NOW() )
I think you need
SELECT
name,
email
FROM users
WHERE DATE(lastModified) = DATE( NOW() )
This effectively "rounds to the date only" and will therefore only match records "since midnight".
I am racking my brain as to how this might work. I have tried a number of variations and can not seem to get accurate results.
I need to check my cdate field which holds the the date and time stamp. Then i need to check for new projects less than 7days and display the row count, then for projects between 7 and 14days and display the row count and finally for projects over 14days old and display the row count.
What i have is a dashboard that shows a number count for those ( NEW / 7+ days / 14+ days )
This is my table field:
cdate
2011-07-29 19:21:29
2011-08-05 19:25:14
2011-08-05 19:25:23
2011-03-29 19:21:29
2011-08-05 19:25:23
2011-08-05 19:25:23
And this is my current queries:
$quotesNEW_sql = mysql_query("SELECT * FROM projects WHERE status=0 AND cdate >= NOW() AND cdate <= NOW() + INTERVAL 6 DAY") or die(mysql_error());
$quotes_result_new = mysql_num_rows($quotesNEW_sql);
$quotesSEVEN_sql = mysql_query("SELECT * FROM projects WHERE status=0 AND cdate >= NOW() AND cdate <= NOW() + INTERVAL 7 DAY") or die(mysql_error());
$quotes_result_seven = mysql_num_rows($quotesSEVEN_sql);
$quotesFOURTEEN_sql = mysql_query("SELECT * FROM projects WHERE status=0 AND cdate >= NOW() AND cdate <= NOW() + INTERVAL 15 DAY") or die(mysql_error());
$quotes_result_fourteen = mysql_num_rows($quotesFOURTEEN_sql);
Thanks in advance :)
John
If all you need is the count, you could use GROUP BY to do it with a single query.
SELECT COUNT(crate), FLOOR(ABS(DATEDIFF(cdate, NOW()) / 7)) AS day_interval
FROM projects
WHERE status = 0
GROUP BY day_interval;
day_interval is broken down as follows:
0 = New
1 = 7-13 days
2 = 14+ days