I have a structural MySQL question about storing events in a database with dates.
Say that an organiser would select a range of dates, eg:
["19/12/2014","20/12/2014","26/12/2014","27/12/2014","02/01/2015","03/01/2015","09/01/2015","10/01/2015"]
The event needs to be saved into a table, I'm thinking about creating a many-to-many table with the structure:
event_id | start_date | end_date
Now when thinking about it, this would mean that I'd need to convert the date array into an array of object with beginning - end date.
Now the alternative would be to just create a table that looks like this:
event_id | event_date
And create a separate record for every date.
The purpose is obviously to check which events should be sent back to the client within a given date range.
Which of the two options seems to common / viable?
It is pretty crucial for the setup.
Depends. If the first event ends on the date of the second event, you can go with event_id | event_date, but otherwise I'd go with the first option.
If you don't have the end date somehow, then how will you be able to tell the client the range of dates for the event?
I would go with setup that contains event duration (in seconds) - it's flexible.
event_id (int) | start_date (datetime) | duration (int)
In this case when event duration does not matter - put 0 there in other case just put the number o seconds so you will be able to store event which lasts days or just a few hours or minutes.
Related
My table has the following columns
| customer_id | service_start_date |
I want to provide the service on alternate week days from the service start date (eg:- every other mondays, every other tuesdays etc..)
If the service_start_date is a monday, then the service will be delivered on every other mondays.
Is there any way to query the mysql table to get all customer_ids who needs service on a particular date?
Try something like
SELECT customer_id
FROM Table as t
WHERE MOD(DATEDIFF(DATE(NOW()), DATE(service_start_date)), 14) = 0
This is assuming that your service_start_date is always a weekday.
Addition to Clami219s answer:
To get the customer(s) that need service on a particular date (as of your request), use
SELECT customer_id
FROM table_name
WHERE
service_start_date <= NOW() AND
WEEKDAY(service_start_date) = WEEKDAY('2014-06-25'); //whatever date you like to fetch
I have a SQLite3 DB with entries storing sensor readings. Each row in the table looks like this:
time | temp1 | humid1 | temp2 | humid2
2013-12-07 23:15:51 | 26.06 | 16.29 | 22.24 | 17.55
where the first entry is the date, and the following ones are sensor data.
Now what I need to do is, using PHP, get the most recent 30 minutes of readings. I only want data that's less than 30 minutes old, since it's possible that data collection wasn't running and I would then get old data if I just selected the first bunch of values.
I can think of 2 ways I could do this:
Is there some way in SQLite to do something like this?
SELECT * FROM table WHERE time < (time-30 minutes)
The other thing I could do is grab the first 300 values (equivalent to 30 minutes)
and then compare the year, month, day, hour, and minute myself. The problem is, I don't know how to extract those from the PHP variable I get when I do just
SELECT * FROM table ORDER BY time DESC limit 300
So let's say I get a row (in PHP) called $row, where the time is $row['time'].
How would I go about extracting the years, months... etc. from that and comparing it to the current time?
You should be able to do something like this:
SELECT * FROM table WHERE time > datetime('now', '-30 minutes');
I'm querying a postgresql database which holds an agenda-table:
agenda |> id (int) | start (timestamp) | end (timestamp) | facname | .....
I want to make a kind of summary of one day in the form of a 'timeline' consisting of a small picture for every 15 minutes interval: on / off according to the availability of the facility.
Now is it relatively simple to query the database for every 15 minutes and check if a reservation is present and change the img source.
But if you want to make an overview of 10 days and 5 different facilities you'll end up querying the database
10(days) * 36(quaters a day) * 5 (facilities) = 1800 database querys/page load.
So this results in a very heavy pay load.
Is there a way I can reduce the amount of queries and so the payload?
To solve this issue, I think we may first find a way to, given a timestamp, find in which quarter of an hour it belongs to. For instance, the hour 08:38 belongs to quarter 08:30, the 08:51 to 08:45, and so on.
To do that, we can use a function like this:
CREATE FUNCTION date_trunc_quarter(timestamp )
RETURNS TIMESTAMP
LANGUAGE SQL
IMMUTABLE
AS $$
SELECT * FROM
generate_series(
date_trunc('hour',$1),
date_trunc('hour',$1)+interval '1hour',
interval '15min'
) AS gen(quarter)
WHERE gen.quarter < $1
ORDER BY gen.quarter
DESC LIMIT 1
$$;
It uses the generate_series function to generate all the four quarters (e.g. 08:00, 08:15, 08:30 and 08:45) within the same hour as the given timestamp (e.g. 08:38), do get the given hour it uses the well-known date_trunc function. Then, it filters only the quarters which is smaller then the given timestamp, sort it and get the bigger one. As it is always only four values at most, sorting it is not a big issue.
Now, with that you can easily query like this:
SELECT date_trunc_quarter(tstart) AS quarter, count(*)
FROM agenda
GROUP BY quarter
ORDER BY quarter;
I think it is fast enough, and to make it even faster, you can create an expression index on agenda:
CREATE INDEX idx_agenda_quarter ON agenda ((date_trunc_quarter(tstart)));
See this fiddle with a self-contained test case of it all.
I'm building a system that shows "events for this month", listed by day and hour.
When I create the event, I set a start date, an end date and a hour.
Let's say that one event starts at 07-10-2011 and ends 07-12-2011. The problem is that some days in this date range will not feature the event. As an example, this event may happen all days and at the same hour, except some few days where it will not happen or has a different hour (think about a show with an opening date different than the rest of the days).
I'm using PHP, MySQL and Codeigniter and my doubt is about the right way to save those dates in the database. Another table with all the dates and the event ID, or save them all in a field inside the event row? Or something else?
Thanks
I'd create two tables. The first table is an events table, and the other is an events_dates table. This way you can create a single event and have as many dates linked to it as you want.
The events_dates table can be as detailed or simple as you want. If it were me, I'd probably have a start_time and end_time column, as well as an event_id and any other data you want.
I would store the range date in a table and then create an "exception" table where you can store your exceptions.
I am trying to create an event calendar with recurring events (ex. weekly or monthly) but I cannot wrap my head around it. Can anyone give me some pointers? What is the best way to go about doing this? Any help is greatly appreciated. Thanks.
Create three tables with a structure like:
event table
-id
-schedule_type
-schedule_id
-title
etc.
schedule table
-id
-event_id
-datetime
schedule_recurring table
-id
-event_id
-date
-time
In your event table, the schedule_type field will be either 0 or 1, which would indicate to the application which table the scheduling information is stored in, as well as how to interpret that information.
A non-recurring event will be stored in schedule with a datetime: 2011-09-06 00:00:00, and recurring events will be stored in schedule_recurring with a date: 'every 1st Monday' and a time: 09:30,12:20 (If the event occurs twice on every first Monday of the month).
Maybe this will help get you started!
I know this is an old post but I was thinking the same thing too.
I like the persons solution about using multiple tables to do it, but in fact it can all be done from one table.
Create a table with the following data...
event_title
event_text
event_image
and_other_fields_about_event
recur_code (text)
recur_mode (integer)
start_date (date)
end_date (date)
recur_end_date (date)
recur_mode can have three states -
0 = no recurrence
1 = recurrence with end date
2 = ongoing with no end date (e.g. if you want to add something like 1st Jan as New Years Day)
recur_code would store either NULL or a code in it if the date recurs. The code that should be used there would be the PHP DateInterval code (i.e. P1Y for 1 year or P3M (3 months) or P7D (7 days), etc) - if you want to shrink the data a bit you could chop off the initial 'P' and add it back later as the initial 'P' is always going to be P, it stands for Period so "Period 3 Months" "Period 7 Days", etc.
Then when your retrieving data from the database - you retrieve all data with the following searches
( end_date >= CURDATE () ) OR ( ( recur_mode = 1 ) AND ( recur_end_date >= CURDATE () ) ) OR ( recur_mode = 2 )
(please note this isn't proper SQL - it's just a basic example of the or statement you'd need)
then once you've retrieved the data use PHP with a while loop and DateInterval to increase the start_date until you get to the next re-occurrence also making sure that if recur_mode is set to 1 the start date is not after the recur_end_date.
All done in one table - and also if you want an input form to put the code in then use a hidden field with the dateinterval value in whilst using various radio buttons to select the interval - then use jQuery onchange to update the hidden value with the new selector values.