I am trying to create a reservation system in php and i have a table(field_data_field_dateres) that has two fields field_dateres_value(start date) and field_dateres_value2(end date). I want to find that if conflict occurs between reservation.
Currently table has a record like this
currently i am writing query like this
SELECT * FROM `field_data_field_dateres` WHERE field_dateres_value>='2014-02-14 20:15:00' and field_dateres_value2<='2014-02-14 20:30:00';
where 2014-02-14 20:15:00,2014-02-14 20:30:00 will come from php side. But its returning empty record. thanks for any help.
Since you want to find times overlapping (conflicting), the query you want is probably instead;
SELECT * FROM `field_data_field_dateres`
WHERE field_dateres_value < '2014-02-14 20:30:00'
AND field_dateres_value2 > '2014-02-14 20:15:00';
Note that the end time is compared to your new time slot's start time, and the start time is compared to your new time slot's end time. This will return all time windows in the database that overlap with your new range.
An SQLfiddle to test with.
Related
I am totally clueless how to get around to get the following kinda result from the same table in MySQL.
Required Result:
The raw data as shown in below image.
Mc_id and op_id can be different. For example, if mc_id is 4 and op_id is 10 then it has to loop through each vouid and extract done_on_date, again it has to loop through for the same mc_id 4 and op_id 10 and extract done_on_date where done_on_date is after first extracted done_on_date. Here second extracted done_on_date, we refer to, as next_done_on_date, just to distinguish it differently. Accordingly continue till end of the table. I hope I am clear enough now.
The idea is basically to see when was particular operation_id carried out for the said machine having mc_id. First time operation done is refered to as done_on_date and when the same operation carried out for the same machine next time, we refer to as next_done_on_date but actually inside the database table it is done_on_date.
Though let me know if anything yet to be clarified
I am trying to set up an sms appointment reminder for client appointments. I use the cms opemEMR. But there do not seem to be a appointment reminder function installed, and no extentions for that function. So I thought it will be possible to do that by filtering out the appointment from SQLi using PHP, and then set up a cron job.
I am new to php and mySQL, and I have been re-thinking how to do it so many times, that by head spins, so I hope some one can show me the right direction.
Here is how I think it can be done:
First I need to go to the calendar table that holds all the calendar events(1), and find the client appointments(2). Then I need to filter the appointments, that scheduled between 24 - 25 hours in advanced(3) (I will then tell the cron job to run every hour).
Then I will need to grab the client id(4) and the time of the appointment.
I will now have client ids on all client, I need to send reminders to.
Second I need to go to the patient data table(5), to grab the phone number(6) from the client ids(7) I just extracted.
I guess, I can then put this data in to another table, from where I can fetch it when running my sms-reminder.
This is a way, I believe would work, but I am no sure how to do it. Hope some one can show me.
Hope it makes sense and that the images help.
Reg.
Lars
Check this query:
SELECT e.pc_pid, e.pc_eventDate, e.pc_startTime,p.phone_cell FROM opememr_postcalendar_events e
LEFT JOIN patient_data p ON p.id = e.pc_pid
WHERE e.pc_Title = 'Office Visit' AND e.pc_eventDate BETWEEN DATE( DATE_SUB( NOW() , INTERVAL 1 DAY ) ) AND DATE ( NOW() )
ORDER BY e.pc_eventDate, e.pc_startTime;
I am selecting records from a table named "bookings" that contains over 100,000 records. I am new to SQL and for some reason this is taking many seconds to finish, and even timing out on my production server:
def bookings_in_date_range(division, startdate, enddate)
sql = "SELECT * FROM bookings WHERE division = '#{division}';"
bookings = ActiveRecord::Base.connection.execute(sql) # all bookings from this division
bookingsindaterange = bookings.select { |b| (parsedate(b["date"]) >= parsedate(startdate)) and (parsedate(b["date"]) <= parsedate(enddate)) } # refine to bookings in date range
end
def parsedate(date) # get date from mm/dd/yy format
d = date.split("/")
return Date.parse("#{d[2]}-#{d[0]}-#{d[1]}")
end
I also included the function I'm using to re-format the date, however executing the SQL statement appears to be where the process is hanging up, based on my tests.
My goal is to select all "bookings" in a "division" within a specified date range. Existing code works faster for divisions with low numbers of bookings.
EDIT
Otávio's code below seems to speed things up a bit. However, my requirement is to see if a booking falls within a date range (on or after startdate and on or before enddate). I can't figure out how to get this logic into the .where statement, so I am running a loop like this:
bookings_start_thru_end = []
(startdate..enddate).each do |date|
date_bookings = Booking.where("division = ? AND date = ?",division, date.strftime("%m/%d/%y"))
date_bookings.each do |b|
bookings_start_thru_end.push b
end
end
Also, the issue with crashing was ActiveRecord session store filling up. I was dumping a bunch of data from the report into the session store to save it between requests to avoid doing another database query, but this was killing performance. The database query is still taking 5 seconds or so, but I can live with that.
Use EXPLAIN to see what the query execution plan is:
https://dev.mysql.com/doc/refman/5.6/en/explain.html
https://dev.mysql.com/doc/refman/5.6/en/using-explain.html
Now my guess is that you do not have indexes on the columns that you are referencing in your WHERE and that leads to table scans which are causing your query to run very slowly. But that is just my guess since I do not know your tables.
The indexes will be required whether you are using raw sql or active record (spit).
Whenever possible, you should avoid executing raw SQL in your applications. Prefer to use ActiveRecord interfaces, this not only will make your app more secure but it will also execute queries in a way in which they are optimized to.
In your case, refactor your bookings_in_date_range method to use ActiveRecord's .where method:
def bookings_in_date_range(division, enddate, startdate)
YourModelName.where("division = ? AND enddate = ? AND startdate = ?",division, parsedate(enddate), parsedate(startdate))
end
To look for things in a range, use
YourModelName.where("division = ? AND enddate <= ? AND startdate >= ?",division, parsedate(enddate), parsedate(startdate))
OK... So I have a calendar, in a custom PHP application built using CodeIgniter, which pulls its entries from a MySQL database. In the table containing the entry data, there are 3 fields that govern the date (start_date), time (start_time) and duration (duration) of the entry. Now, as the end-user moves an entry to a new date and/or time, the system needs to check against these 3 fields for any schedule conflicts.
Following are some vars used in the query:
$request_entry_id // the id of the entry being moved
$request_start_date // the requested new date
$request_start_time // the requested new time
$request_duration // the duration of the entry being moved (will remain the same)
$end_time = ($request_start_time + $request_duration); // the new end time of the entry being moved
My query used to check for a schedule conflict is:
SELECT t.id
FROM TABLE t
WHERE t.start_date = '$request_start_date'
AND (j.start_time BETWEEN '$request_start_time' AND '$end_time'))
AND t.id <> $request_entry_id
The above query will check for any entry that starts on the same date and time as the request. However, I also want to check to make sure that the new request does not fall within the duration of an existing entry, in the most efficient way (there's the trick). Any suggestions? Thanks in advance!
It's easier to figure out the logic if you first think about the condition for when there is no conflict:
The new event ends before the existing one starts, or starts after the existing event ends.
For there to be a conflict we take the negative of the above:
The new event ends after the existing one starts, and starts before the existing event ends.
In SQL:
SELECT t.id
FROM TABLE t
WHERE t.start_date = '$request_start_date'
AND ('$end_time' > t.start_time AND '$request_start_time' < addtime(t.start_time, t.duration))
AND t.id <> $request_entry_id
I have a "last_action" column in my users table that updates a unix timestamp every time a user takes an action. I want to select all users in my users table who have made an action on the site after a specific time (probably 15 minutes, the goal here is to make a ghetto users online list).
I want to do something like the following...
time = Time.now.to_i - 900 # save the timestamp 15 minutes ago in a variable
User.where(:all, :where => :last_action > time)
What's the best way to do this? Is there a way of doing it without using any raw SQL?
Maybe this will work?
User.where("users.last_action > ?", 15.minutes.ago)
Try this:
time = Time.now.to_i - 900
User.where("last_action > #{time}")
There might be a nicer/safer syntax to put variable arguments in to the where clause, but since you are working with integers (timestamps) this should work and be safe.