MySQL Insert if selected value > x
I am trying to write a MySQL insert that only inserts based on the result of a select query. I have found some information on using if-then-endIf logic in queries but haven't had any luck getting one to work. Then general idea that I am trying to accomplish is like:
if
result of select is > 0
perform insert and select true
else select false
endIf
I want to run it this way as it seems like the easiest way to stop from inserting more than the predefined limit.
Anyway, if someone could point me in the right direction for how to structure/nest the statements I would be very gratefu开发者_StackOverflow社区l.
Thank you.
EDIT
in case anyone is wondering, this is what I ended up with:
insert into users_to_classes select ? , ?, 4 from (select class_id, count(*) as students from users_to_classes where participation_level=4 group by class_id) as numUtC, classes where numUtC.class_id=classes.class_id and classes.class_id=? and classes.spaces - ifnull(numUtC.students,0) > 0
where it is used as a mysqli prepared statement.
Thanks again for the help
You could try this with some relevant changes:
INSERT INTO <destination_table>
SELECT <values>
FROM <source_table>
WHERE <value> > x
Slight edit to make it clearer.
精彩评论