简体   繁体   中英

MySQL Stored Procedure that select, check dates and insert

I am trying to create a stored procedure in MySQL. I have never used one so I don't even know if it's possibile to do what I need.

I have two tables :

  1. the first contains jobs that are already done (id_operator, date, payment_days, payment_status..)
  2. the second one contains the notifications that are sent to the operators (id, text, id_operator..)

I need to select all the records from the first table that have 0 in the payment_status field, check if the date + payment_days exceeds the current date and insert a notification in the other table with the id of the operator.

I hope you can help me, thanks.

You can do this:

INSERT INTO notifications(text, id_operator)
SELECT 'notification text', id_operator
FROM jobs
WHERE payment_status = 0
  AND CURRENT_DATE <= DATE(DATE_ADD(`date`, INTERVAL payment_days DAY));

Using INSERT INTO ... SELECT ... you can select the results and insert them into another table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM