I can’t seem to get the syntax right. I’m looking to check a table for a value before allowing another record to be inserted.
Something like this:
IF EXISTS (SELECT * FROM rooms WHERE id = 1)
THEN
INSERT INTO other_table (room_id) VALUES (1)
END IF
I just want to make sure a particular record exists before it’s allowed to be entered in another table… To go even further, I’d actually like to make sure a couple things exist…
IF EXISTS (SELECT * FROM rooms WHERE id = 1) AND (SELECT * FROM guests WHERE guest_id = 10)
THEN
INSERT INTO other_table (room_id,guest_id) VALUES (1,10)
END IF
I’m not up on transactions yet, or stored procedures, so if I need to learn all of that first, I’m going to be sad.
You can use
INSERT INTO ... SELECT:(
GROUP BYjust to make sure the subquery returns no more than 1 row; ifrooms.idis unique, you don’t need it.For multiple tables: