MySQL runs automated scripts
P粉821231319
2023-08-18 11:24:32
<p>I have a MySQL database with two tables: drivers and devices</p>
<p>The drivers table has a field called expiration date.
The devices table has a field called status. </p>
<p>My goal is to create an event that will:</p>
<ul>
<li>Get the current date</li>
<li>Compare drivers (expiration date) with current date</li>
<li>If the expiration date has passed, you need to change the status of the device to 'EXP'</li>
</ul>
<p>Is such a thing possible?
Like an expiration check</p>
This is the query your event needs to execute:
As for event creation, you have several options, some of which are listed here:
Regardless, you may want to wrap your
UPDATE
in a stored procedure, and if there are more writes, you may also want to call this event when Wrap a transaction.Okay, consider the following
MySQL
table structure:You need to go through each driver to see if it has expired and update the device correctly.
First, you need to enable
EVENTS
in the database:Next, you can create an
event
that runs daily, checking allactive
devices forexpired
drivers and updating them appropriately: