How to select dates from mysql table 'Courses' between now and 4 weeks later where Open='1' and sort by Date and Time?
SELECT * FROM Courses
WHERE Open='1' AND Date BETWEEN CURDATE() AND CURDATE() + INTERVAL 4 WEEK
ORDER BY Date, Time
Not tested
Related
I currently use DATE_SUB to show results in a database. So for data dating back 2 months I will do;
SELECT * FROM table WHERE `date` >= DATE_SUB(CURRENT_DATE, INTERVAL 2 MONTH)
For data dating back 20 days, I will do
SELECT * FROM table WHERE `date` >= DATE_SUB(CURRENT_DATE, INTERVAL 20 DAY)
And for data dating back 2 years, I will do
SELECT * FROM table WHERE `date` >= DATE_SUB(CURRENT_DATE, INTERVAL 2 YEAR)
What I can't figure out is how to get data for a specific year eg if I want data for 2015 only. How do I do that?
NB. date column type is datetime
You could use year()
SELECT * FROM table WHERE year(date) = 2015
I want to get all records from the last x months from my MySQL server. Using 2 months for example(Not from last 2 months, like last 60 days, but from the entire past month and so on. If actual month is april, I want all records from february and march).
I've tried some queries and the last one is
SELECT id FROM ranking WHERE (end_date BETWEEN DATE_FORMAT(NOW(), '%Y-%m-01') AND DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 2 MONTH), '%Y-%m-%d'))
"end_date" is my date column which is a DATE column "2017-04-07".
That query above just returns nothing and I cant figure out where is the error.
Try this:
SELECT id
FROM ranking
WHERE end_date BETWEEN
DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 3 MONTH)) INTERVAL 1 DAY)
AND LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH));
It calculates dates as like this:
For start date in range, it subtracts 3 months from current date, gets the last day of that month and adds a day in it to get the first day of next month (1st February in our case)
For end date, it subtracts one month from current date and gets the last day of that month (31st March in our case)
Try without convert it to string, And BETWEEN requires min date first.
SELECT id
FROM ranking
WHERE end_date BETWEEN LAST_DAY(CAST(NOW() as date) - INTERVAL 2 MONTH
AND CAST(NOW() as date)
I have two columns(date field, time field) which are storing date and time in string and first step is to combine these two fields so i can compare them with current date(i'm not sure if i'm doing that right). Second step is writing query which should select records only if obtained date from these two columns is close to current date. And that date should be between current date and 4 hours before current date because after that something will happen with all those records if some conditions are not met.
Format of columns date and time:
event_date = 'yyyy/MM/dd'
event_time = 'HH:mm'
So here is my query:
SELECT *
FROM `events`
WHERE DATE_SUB( NOW( ) , INTERVAL 4 HOUR )
< ADDTIME( event_date, event_time )
Maybe i should firstly convert these strings to date and then work with them or something else? All in all if i have some data with these dates for example:
1. 2017-02-26 10:00
2. 2017-02-26 11:00
3. 2017-02-27 10:00
And current datetime is: 2017-02-26 06:00
. In this case i would get only 1 record and when one hour pass or 2017-02-26 07:00 then i will get 2 records
If event_date is the date of the event and event_time is the time, you could combine them using the timestamp mysql function:
select event_date, event_time, timestamp(event_date, event_time)
from events;
but it's usually a good idea to actually store the date and time information in a single field e.g. event_datetime
you can then write your query like this:
select *
from
events
where
event_datetime between now() and now() + interval 4 hour;
this will return all events starting from now and 4 hours in the future. Or if you want the events from the last 4 hour in the past try with:
event_datetime between now() - interval 4 hour and now();
You should convert your date and time to a proper datetime data type. That said, you can do something like the following to accomplish what you want:
SELECT * FROM `events`
WHERE STR_TO_DATE(CONCAT(event_date,' ',event_time), '%Y/%m/%d %H:%i')
BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 4 HOUR)
The query will return all rows between now and 4 hours from now.
UPDATE: I just noticed the answer from #fthiella, using timestamp probably gives better performance than the example I provided.
I am trying to fetch rows only from specific date (like today, yesterday or 2 days ago) in mySQL. I have a column named "date" in my rows. (which includes dates like 1365053426).
$result=mysql_query("SELECT count(*) as total from track WHERE `date` >= CURRENT_DATE
AND `date` < CURRENT_DATE + INTERVAL 1 DAY");
I have tried this query, but it returns "0". What is the correct way to do that ?
how about using BETWEEN?
SELECT COUNT(*) as TotalCount
FROM Track
WHERE Date BETWEEN CURDATE() + INTERVAL -2 DAY AND CURDATE()
How about using datediff() function?
SELECT count(*) as total from track WHERE datediff(now(),date)=interval day
note: interval day could be declare from 0 -> up depends on what previous date you want to show
I am using PHPMyadmin and putting values in a database using PHP. I store the expiry date of products using a timestamp as follows,
FOR EXAMPLE:
2012-11-04
I want to select all where the expiry date is equal to todays date plus 8 days (such as the one above)
I also want to select all where expiry date is equal to todays date + 2 weeks in a seperate page if any one could help me out would be very grateful!
You can do that with a query like this:
SELECT * FROM table WHERE date = DATE_ADD(CURDATE(), INTERVAL 8 DAY)
You can use DATE_SUB for dates in the past.
Select all where the expiry date is equal to todays date plus 8 days
SELECT
*
FROM
products
WHERE
products.expiry_date >= DATE(now())
AND
products.expiry_date <= DATE_ADD(DATE(now()), INTERVAL 8 DAY)
Select all where the expiry date is equal to todays date plus 2 weeks
SELECT
*
FROM
products
WHERE
products.expiry_date >= DATE(now())
AND
products.expiry_date <= DATE_ADD(DATE(now()), INTERVAL 2 WEEK)
These docs will be helpful for you:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add