简体   繁体   中英

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;

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