开发者

IF Statement in MySQL

开发者 https://www.devze.com 2023-03-25 09:34 出处:网络
I would like to insert a row in a table if a variable has a certain value. I have tried the following:

I would like to insert a row in a table if a variable has a certain value. I have tried the following:

select foo from bar into @tempvar;

if @tempvar = '1'
begin
    insert into t1 (f1, f2) values (v1,v2);
end
end if;

Alas, this d开发者_如何学运维oes not work. Any idea how to do this?


INSERT INTO t1 
  (f1, f2)
SELECT 
  v1, 
  v2 
FROM 
  bar 
WHERE 
  foo = 1

v1 and v2 can be literal values (numbers, strings, etc), they don't have to be columns from bar. You will get as many INSERTs as the SELECT portion returns.

0

精彩评论

暂无评论...
验证码 换一张
取 消