我正在尝试编写一个仅基于选择查询结果进行插入的 MySQL 插入。我找到了一些关于在查询中使用 if-then-endIf 逻辑的信息,但没有任何运气可以让其工作。然后我想要完成的一般想法是:
if
result of select is > 0
perform insert and select true
else select false
endIf
我想以这种方式运行它,因为它似乎是阻止插入超过预定义限制的最简单方法。
无论如何,如果有人可以为我指出如何构建/嵌套语句的正确方向,我将不胜感激。
谢谢你。
编辑
如果有人想知道,这就是我最终得到的结果:
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
它用作mysqli准备好的语句。
再次感谢您的帮助