簡體   English   中英

如果語句使用INSERT作為then子句(MYSQL)

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

我正在做一個有條件的插入,但是我在做一個有困難。

條件是這樣的

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

您可以這樣嘗試:

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

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

或喜歡

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

嘗試這個:

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