selecting current match from the cricket schedule using mysql query - mysql

I have a cricket schedule and i need to select the matches of the current day just half an hour before the match starts and the selection should last until the 2 hours past the end of the match , this should be done in a single query i have used the following query but it is not working accurately...
SELECT * FROM `schedule` WHERE
date1 BETWEEN DATE_SUB(NOW(), INTERVAL 30 MInute)
AND DATE_ADD(NOW(), INTERVAL 10 HOUR)

If you want to display all scheduled matches from half an hour before they start until 2 hours after they end, and all mathces have a duration of 12 hours, then you need to show all matches within a 14,5 hours interval, and not a 10,5 hours interval as your current code does:
SELECT * FROM `schedule`
WHERE NOW() BETWEEN DATE_SUB(date1, INTERVAL 30 Minute)
AND DATE_ADD(date1, INTERVAL 14 HOUR)
Furthermore, you should compare the current time (NOW()) to the start time and end time of the game, not the other way round.

Related

How to get last 1 hour to last 10 days data in mysql

I need to get data within a range which starts a hour ago. As an example if now time is 08.00 AM, I need to get all the data within 07.00 AM to last 10 days. I have tried with datesub function as below, But seems this is wrong because the date(now()) and date(now()-interval 1 hour) are both same.. Can someone show me how to get this?
.....and date(time)>date(date_sub((now()-interval 1 hour), interval 10 day)) group by ds.....
Assuming time field in your query is either timestamp or datetime.
... and time between cast(concat(date_sub(curdate(), INTERVAL 10 day), ' 00:00:00') as datetime) and DATE_SUB(now(), INTERVAL 1 hour)
Hope this will help.

select specific time from previous day

I basically want to select data between 7pm the previous day and NOW(). I'm not sure the best practice or most efficient way to do this on an automated report generated by a query I could write.
SELECT * FROM table WHERE timestamp BETWEEN curdate() - INTERVAL 1 DAY AND
NOW()
How do I get the curdate() - interval 1 day to start at 7pm of the previous day?
curdate() returns just the date, e.g. 2018-03-21. Time in this case is omitted, but would be 00:00:00. To start at 07:00 pm just add another 19 hours like this:
select curdate() - INTERVAL 1 DAY + INTERVAL 19 HOUR;

sql query to fetch the records of next 30 days

Here is my problem, I want to fetch next 30 days records from the table. I have a field in my table. For ex: In my table I have resource_date, In this column I have many records from 2013-02-05 to 2015-10-10. Say, If I logged into the website today(Today's Date is- 16/01/2015, It should fetch record for next 15 days and so on). How to do this? Thanks in advance
One way to do it
SELECT *
FROM table1
WHERE resource_date >= CURDATE() + INTERVAL 1 DAY -- skip today
AND resource_date < CURDATE() + INTERVAL 17 DAY -- 15 days starting tomorrow
Here is a SQLFiddle demo
In MySQL, you can use the NOW() function to get the current DATETIME, and the INTERVAL keyword to get intervals of time.
So, to get the records where resource_date is within the next 30 days, you would use:
SELECT *
FROM `my_table_name`
WHERE `resource_date` >= NOW()
AND `resource_date` < NOW() + INTERVAL 1 MONTH
;
In practice, you should rarely use SELECT *, and you should consider adding a LIMIT to this query to prevent your application from returning a result set that is "too large".
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
...
WHERE
'resource_date'> NOW() AND
'resource_date'< DATE_ADD(NOW(), INTERVAL 31 DAY);
Careful I think now() does minutes and hours so you miss a portion of a day.
WHERE resource_date >= CURDATE() AND resource_date <= DATE_ADD(CURDATE(), interval 15 DAY)

Data from current date to 30 days after current days

Why this query is not working
SELECT * FROM history WHERE DATE(date) < CURDATE() + 30
I am trying to get the data from 30 days but my query is not working.Why
What does +30 mean? Days? Years? Months? Hours? You need to use (the proper syntax) a format MySQL understands:
SELECT * FROM history WHERE DATE(date) < CURDATE() + INTERVAL 30 DAY
To get the data from today on to 30 days after current day, you've got to set an upper and an lower limit, so use:
SELECT * FROM history WHERE
date >= CURDATE()
AND
date < CURDATE() + INTERVAL 31 DAY
Please note that by not using a function on your date column you won't prohibit MySQL to use an index on this column.
The lower limit should be obvious, the upper limit means that you've got the complete day that's 30 days later than today. If you use + INTERVAL 30 DAY instead this last day is excluded from the result.
Because you're not using the right construct, try:
SELECT * FROM history WHERE DATE_ADD(date, INTERVAL 30 DAY);

MySQL intervals dates remaining

I have to write a query that pulls the users that are ten days away from ending their free trial. My question is, do i do a minus 10 day interval or a plus 10 day interval against CURRENT_DATE().
I am having difficulty thinking about this.
Here's my query:
SELECT * FROM users WHERE freetrial=1 AND date_format(date_created,'%Y-%m-%d 00:00:00') = CURRENT_DATE() - INTERVAL 10 DAY
If the trial period is 30 days long then you need to add 20 days to the date they registered and compare that to today's date.
SELECT *
FROM users
WHERE freetrial=1
AND FROM_UNIXTIME(date_created,'%Y-%m-%d') + INTERVAL 20 DAY = CURRENT_DATE()