I have two tables; bookings
and tasks
. Each booking has 1-to-many tasks.
bookings
table:
+--------------+----------------+------+-----+----------------+
| Field | Type | Null | Key | Extra |
+--------------+----------------+------+-----+----------------+
| bookingsID | int(8) | NO | PRI | auto_increment |
| clientID | int(8) | NO | FK | |
| vehicleID | int(8) | NO | FK | |
| date | date | NO | | |
| complete | tinyint(1) | NO | | |
+--------------+----------------+------+-----+----------------+
tasks
table:
+--------------+----------------+------+-----+----------------+
| Field | Type | Null | Key | Extra |
+--------------+----------------+------+-----+----------------+
| taskID | int(8) | NO | PRI | auto_increment |
| bookingID | int(8) | NO | FK | |
| description | text | NO | | |
| price | decimal(10, 2) | NO | | |
| complete | tinyint(1) | NO | | |
+--------------+----------------+------+-----+----------------+
I would like to create a trigger so that when the complete
field for each task is set to TRUE, the complete
field of the related booking is also set to TRUE. I’m not sure how to check all tasks
.complete
fields for a specific bookingID
.
2
Answers
For this part, how about checking that the count of rows where complete = False is 0:
Here is a sample with an AFTER UPDATE TRIGGER.
You will also need a AFTER INSERT TRIGGER with the same code
fiddle