Create a mysql event with condition - mysql

I currently have a mysql database where I keep data about rented books. When somebody rents a book, I save on the database an expiration date.
So, I want to create an mysql event to update the book status when it is not returned in time. How can I create this event?
Here's what I'm trying:
CREATE EVENT `EXPIRAR` ON SCHEDULE EVERY 1
HOUR STARTS '2016-07-16 00:00:00.000000' ENDS '2018-07-29
00:00:00.000000' ON COMPLETION NOT PRESERVE ENABLE DO DO IF
mtl_aluguel.dataLimite=CURRENT_DATE THEN
UPDATE mtl_aluguel SET mtl_aluguel.estado='Expirou'
END IF
But I'm getting Syntax errors on the IF.
mtl_aluguel is the name of my table.

Like #Drew said, my event doesnt make any sense. So I updated it:
CREATE EVENT `EXPIRAR` ON SCHEDULE EVERY 1
HOUR STARTS '2016-07-16 00:00:00.000000' ENDS '2018-07-29
00:00:00.000000' ON COMPLETION NOT PRESERVE ENABLE DO UPDATE mtl_aluguel SET mtl_aluguel.estado='Expirou' WHERE mtl_aluguel.dataLimite = CURDATE() AND mtl_aluguel.estado!= 'Expirou'

Related

Filtering SQL tables

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;

How to create a trigger that resets values to zero after 30 days?

I have a table 'users'. In that table there is a field balance whose datatype is ENUM('Yes','No'). Now I want to create a trigger that is fired when the balance is set to "Yes". Also I want to reset the value of Balance to NO after 30 days.
I am using PHP Codeigniter. And I have a reseller table who has its own users. So Each reseller has some users.
So when a reseller sets the balance of user to yes the trigger should
be fired and after 30 days should set user balance to NO
Also the balance of each user should be Reset uniquely. I mean one uses's balance is set to yes today and user-two balance is set to Yes tomorrow . So how will trigger know when to fire for that each specific user?
My solution is
Maintain a column like updatedon.
Write a stored procedure to set balance to NO when updatedon is 30 days and balance is YES(according to your critaria)
Run a Job daily to invoke the stored procedure(It will set only 30 days back updated records)
In order to track date you need to keep a column on which user updated_to_yes_date. When ever user update that enum column to "Yes" you should set current date to updated_to_yes_date.
Now lets code the controller for cron job.
class Cronjob extends CI_Controller {
function index() {
// Get Date before 30 days.
$today = new DateTime();
$dateBefore30days = $today->modify('-30 days');
$this->db->update('users',array('enumcolumn'=>'No'), array('enumcolumn'=>'Yes', 'updated_to_yes_date'=>date_format($dateBefore30days , 'Y-m-d'));
}
}
I have not tested above controller but it will help you.
Now its time to set up cron job. You need to run this cron job every day. To know how to setup cron job on cPanel CLICK HERE
Now we need to execute controller method from command line. For that go to codeignitor Running via the CLI document.
In our case it will be like
$ php index.php cronjob index
In many case we need to provide full path to index.php like
$ php /path/to/webroot/index.php cronjob index

Between two datestime in mysql

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.

Check for a date/time conflict MySQL

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

MySQL query for checking event conflict

Currently I making a school community site and I got a problem in a part were the administrator post a new event he/she wants check if the event start date and time has no conflict also end date and time before saving it on the database. I tried many queries still i fail to get the exact sql.
I'm using MySQL Server 5 and PHP 5.3. Can any one help me on how to query on the table i mean the right sql string.
Heres the table fields:
event_id => int,
event_type => enum('school','department'),
event_title=> text,
event_details=> longtext,
event_start=> datetime,
event_end=> datetime,
event_added_by=> int,
event_modified_on=> datetime,
Here is what i have right now:
SELECT *, time(event_start) as start_time, time(event_end) as end_time
FROM `tbl_events` WHERE ((date(event_start) between '2011-12-9' and
'2011-12-15') AND (time(event_start) between '00:00:00' and
'01:00:00')) OR ((date(event_end) between '2011-12-9' and
'2011-12-15') AND
You have four things to look for that indicate a conflict:
an existing event starts before your event starts and ends after your event ends (your event is inside the existing event)
an existing event starts before your event starts and ends after your event starts (overlap the beginning of the new event)
an existing event starts before your event ends and ends after your event ends (overlap the end of the new event)
an existing event starts after your event starts and ends before your event ends (the existing event is inside the new event)
By before and after, I mean at the same time or before/after (<= and >=).
There are two checks to do that need to be combined into one condition:
an (existing) event starts before your event ends
and
it ends after your event starts.
If the query with such a condition returns rows, there's a conflict.
I'm assuming here that the end time is the time when the event ceases to be active (or functional or valid or whatever). That is, if an event's start time equals another event's end time, there's no conflict in there. However, if that should be considered a conflict in your case, replace the before and after above with before or when and after or when respectively.
Here's how it might look in a query:
…
WHERE event_start < #new_event_end
AND event_end > #new_event_start
…
Or, if no event_start should match another event's event_end:
…
WHERE event_start <= #new_event_end
AND event_end >= #new_event_start
…
(That is, simply replace strict inequalities with non-strict ones.)
This allows also for open ends (modeled by end = null):
WHERE (event2.start < event1.end OR event1.end is null)
AND (event2.end > event1.start OR event2.end is null)
And take care whether using > or >= (respectively < or <=), this depends on your data and requirements.