简体   繁体   English

如果语句使用INSERT作为then子句(MYSQL)

[英]if statement with INSERT as then clause (MYSQL)

I am making a conditional insert but i having trouble making one. 我正在做一个有条件的插入,但是我在做一个有困难。

the condition is something like this 条件是这样的

if(select count(*) from employee < 3, insert into employee (name) values ("lisa");

You can try like this: 您可以这样尝试:

SET some_var = (select count(*) from employee);

if(some_var < 3) then
    insert into employee (name) values ("lisa");
end if

or like 或喜欢

SELECT COUNT(*) AS count into @some_var FROM employee;
if(@some_var < 3) then
    insert into employee (name) values ("lisa");
end if;

Try this: 尝试这个:

insert into employee(name)
    select 'lisa'
   where (select count(*) from employee) < 3;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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