I dont know if this is even possible. I have a MySQL table containing season start and end dates for advertisers. The dates dont change from year to year so they are stored in MM-DD format.
These are normally consective within a year , ie March 1st (03-01) to October 1st. (10-01).
BUT the seasons may wrap around a year end, eg October 1st. (10-01) to March 1st (03-01) (ie they are closed in the spring & summer)
Now I need to select entries (via PHP) where a date range entered by a user ( say December 1st to December 12th.) falls within advertiser season start/end dates.
Easy enough if the season start/end were mathematically consecutive ( eg 03-01 - 10-01) but how do I do it when they may or may not be consecutive.
Sorry if I havent explained that clearly but my head's about to explode....
Use the proper column type (DATE) for this data, and the season wrapping becomes a non-issue.
To get the season(s) that the user-supplied date range applies to:
SELECT foo
FROM seasons_table
WHERE <user range low> >= season_range_low
AND <user range high> <= season_range_high
How about:
SELECT *
FROM table
WHERE CASE
WHEN date_start > date_end THEN
$date_to_check >= date_start OR $date_to_check <= date_end
ELSE
$date_to_check >= date_start AND $date_to_check <= date_end
END;
you should be able to use an AND statement in your SQL to check for the two possibilities:
SELECT * FROM table WHERE
season_start_date <= advertiser_start_date AND
season_end_date >= advertiser_end_date OR
No matter what the season end date needs to be a higher value than the ad end date, even if it's in the new year. So that should work, but I agree with #SoboLAN, it would be much simpler if you just add the year to the date fields.
Related
how to search between two date , when date format in database like :
2015-10-10 02:23:41 am
i just want to search between two date with format :
2015-10-10
without
02:23:41 am
any ideas please ?
Your question isn't completely clear. But, I guess you hope to find all the rows in your table where Date occurs on or after midnight on 2015-08-05 and before midnight on 2015-09-11 (the day after the end of the range you gave in your question.
Those two search criteria will find all the rows with Date values in the range you specified. (I'm ignoring the 02 at the end of 2015-09-10 02 in your question because I can't guess what it means, if anything.)
Try this query:
SELECT *
FROM table
WHERE `Date` >= '2015-08-05'
AND `Date` < '2015-09-10' + INTERVAL 1 DAY
This has the benefit that it can exploit an index on the Date column if you have one, so it can be fast.
You could write
SELECT *
FROM table /* slow! */
WHERE DATE(`Date`) BETWEEN '2015-08-05' AND '2015-09-10'
That's slightly easier to read, but the WHERE condition isn't sargable, so the query will be slower.
Notice that the beginning of the range uses >= -- on or after midnight, and the end of the range uses < -- before midnight.
Pro tip: Avoid the use of reserved words like DATE for column names. If you make mistakes writing your queries, their presence can really confuse MySQL, not to mention you, and slow you down.
May I suggest:
select * from table where cast(date as date) between '2015-08-05' and '2015-09-10'
When your where clause is based on a timestamp, but you're using date as the parameters for your between, it excludes anything that happens on the second date unless it happened precisely at midnight.
When using the end date for the range, include the time of the end of the day:
SELECT *
FROM YourTable
WHERE date BETWEEN '2015-08-05' AND '2015-09-10 23:59:59'
I am trying to abstract querying a month of data on the MySQL level without having to implement a series of conditions that determine the last day of a given month on a given year. LAST_DAY() seemed to be the answer but it appears to only return the date and not the provided time.
SELECT LAST_DAY('2011-02-05 23:59:59');
returns
2011-02-28
When I try to use it in a query, I lose all the entries from the last day of the month because without time the date value is not accepted.
SELECT * FROM subscriptions
WHERE (`modified_at` BETWEEN '2014-12-01 00:00:01' AND LAST_DAY('2014-12-01 23:59:59'));
How can I modify this query so that the LAST_DAY function either generates the last time of the day or preserves the time given?
How about changing the logic to ignore times?
SELECT *
FROM subscriptions
WHERE `modified_at` >= '2014-12-01' AND
`modified_at` < date_add('2014-12-01', interval 1 month)
And this doesn't need last_day().
Use ADDTIME(expr1,expr2);
Like this:
ADDTIME(LAST_DAY('2011-02-05 23:59:59'), '23:59:59')
I want to compare a datetime from sql and the todays date.
The datetime in sql is for example 2015/05/09 12:00:00 and the current date is 2015/05/09. I want to compare these things if they are equal, but for the whole day so the time is a problem. I have the following query.
$q1="SELECT reservations.reservation_id, reservations.room_id, room.room_rate, reservations.arrival_date, reservations.departure_date,
meals.meal_rate, room.room_rate, roomtype.max_persons,
CONCAT(clients.first_name,' ',clients.last_name)as name
FROM reservations, clients, meals, room, roomtype
WHERE reservations.room_id=room.room_id AND reservations.client_id=clients.client_id AND room.roomtype_id=roomtype.roomtype_id
AND reservations.meals=meals.meal_id AND reservations.arrival_date=CURDATE()
GROUP BY reservation_id
";
reservations.arrival_date=CURDATE() this is equal only at 12:00:00 o'clock. i want that to be equal for the whole day.Actually i want to compare if these two dates are equal without time but my database must be datetime with the time...
does anyone has an idea?
thanx in advance
You should learn to use standard join syntax. Simple rule: Never use commas in the from clause.
The answer to your question is either the date() function (or something similar):
where date(reservations.arrival_date) = CURDATE()
Or an inequality:
where (reservations.arrival_date >= CURDATE() and
reservations.arrival_date < date_add(CURDATE(), interval 1 day)
)
The second is actually preferred because it can make use of an index on reservations(arrival_date).
Evrey row in my table has a date Y-m-d. Now I want to select only those rows that have a date from before a certain month and year independet of the day. So for example I want all rows with a date befor january 2014.
How would I do this in MySQL? Only considering the month does not work, because it will give me also rows with a month smaller, but a year after my year. E.g. i want rows with date smaller than 03/2013. It will give me also 02/2014.
Of course I could in php define first the first day of the month and than compare the full date. But this does no seem too great and when I want to compare dates after a certain month I have to know how many days a month has.
You can use the STR_TO_DATE function to convert to actual DATE type and then do a simple comparison to the cut-off date.
SELECT *
FROM Your_Table
WHERE STR_TO_DATE(Your_Date_Column,'%Y-%m-%d') < STR_TO_DATE('2013-03-01','%Y-%m-%d');
I am currently trying to write a little program to track time-off requests for employees. I'm fairly new to MYSQL and PHP, so it's a learning project for me as well. I've run into this problem which I do not seem to be able to figure out.
I want to display time off requests for a given week (Mon-Fri). I've got the requests in a table 'requests', with a 'Starttime' and 'Endtime' in separate fields, both Date/Time.
I can currently easily search and retrieve requests that have either (or both) Starttime or Endtime values that fall within the given ISO week I am looking at (WEEKOFYEAR() ).
What I need to be able to do is search for requests that may include days in the ISO week I am displaying, but not have a Starttime or Endtime during that week.
Example:
Employee takes off Tuesday of Week 24 through Friday of Week 24.
Currently, I would correctly display that the employee was off starting Tuesday and show a return on that Friday, but on Wed and Thursday nothing would be entered.
Employee takes off Friday of Week 30 through Monday of Week 32.
Currently, I would show that employee as not being 'off' during Week 31 because the search would not show a Starttime or Endtime during that week even though they are actually off the entire week. Though the Starttime and Endtime would be noted on the correct days.
Right now, what I do to work around this is run 5 additional queries to check if the date for each day Mon-Fri during Week 31 is contained BETWEEN the Starttime and Endtime of each request in the db.
I hate to run a total of 6 queries to get this information. Is there an easier way to get that information?
I just wrote a calendar app for events and ran into this- how bout something like this:
SELECT * FROM Requests WHERE
Start_Date BETWEEN <first_day_of_week> AND <last_day_of_week>
OR
End_Date BETWEEN <first_day_of_week> AND <last_day_of_week>
OR
<day_of_week_monday> BETWEEN Start_Date AND End_Date
OR
<day_of_week_tuesday> BETWEEN Start_Date AND End_Date
OR
<day_of_week_wedenesday> BETWEEN Start_Date AND End_Date
OR
<day_of_week_thursday> BETWEEN Start_Date AND End_Date
OR
<day_of_week_friday> BETWEEN Start_Date AND End_Date
GROUP BY ID ORDER BY Start_Date ASC, Date ASC
While < value_names > are generated with php via the currently viewed week requested. Should cover your bases.
What format are these dates stored in the DB? Assuming they are using MySQL's DATE format, its pretty easy to do. You can just use comparison operators on the fields and MySQL will do the work for you in one query.
$sql = "SELECT * FROM table WHERE startdate <= $someday AND $someday <= enddate";
Sticking to your week of year way of doing it, you can run a check to see if the current week falls within the range of the starting week off and the ending week off.
SELECT * FROM table WHERE WEEKOFYEAR(Starttime) <= N AND WEEKOFYEAR(Endtime) >= N;
(where N is the week you're displaying)
What you'd want to do, once you get the rows, is parse each day in PHP to see if that day falls between the starttime and endtime.
A good method for that is using:
$start_timestamp = strtotime($row['Starttime']);
$end_timestamp = strtotime($row['Endtime']);
You can use a similar method to get a timestamp of the day you are displaying, and see if it falls between $start_timestamp and $end_timestamp to determine if that day is off.
since you mentioned PHP as the display:
$start=30; //Friday of Week 30
$end=32; //Monday of Week 32
foreach (range($start, $end) as $number) {
echo $number;
}
You would expect to get 30,31,32.
You will have to verify that $start is < $end though as well for December to January weeks.
The range for WEEKOFYEAR() is 1-53. In this case, add 53 to the $end and display mod of $end if greater than 53:
$start=52; //Friday of Week 52, 2009
$end=2; //Monday of Week 2, 2010
If($start>$end) $end+=53;
foreach (range($start, $end) as $number) {
if($number>53){
echo $number%53;}
else{
echo $number;
}
}
You would expect to get 52,53,1,2
Probably need the query to read either like the big one with all the ORs or something like this
WHERE
Starttime <= <value for last day/time of the week>
AND
Endtime >= <value for the first day/time of the week>
That should get all the dates you need that could fall in that week, then you parse the records you get with PHP to find the ones for that actual week... though there may be an easier way to do that.