0

我想SELECT在条件等于 1 时做语句,这样就会执行INSERT语句,否则,它不会做任何事情。我认为您可以在我的代码中看到下面的内容。

SET @var = 1;

SELECT IF(@var = 1, 
    INSERT INTO tabulka VALUES('value1','name1'),
    0
)

谢谢

4

2 回答 2

1

您只需要一个 IF 语句,而不是一个 SELECT。

if
    @var = 1
then
    insert into tabulka values('value1', 'name1');
end if
于 2013-11-02T19:55:18.957 回答
0

知道了!

http://sqlfiddle.com/#!2/66b33/1/0

create table test (
    int_field int, 
    str_field varchar(10) 
  );


set @var := 1;

insert into test 
   select  int_field, str_field 
   from    (select @var as int_field,
                   concat('var=',@var) as str_field 
           ) as temp
   where   @var=3;


set @var := 3;

insert into test 
   select  int_field, str_field 
   from    (select @var as int_field,
                   concat('var=',@var) as str_field 
           ) as temp
   where   @var=3; 


select * from test; 

结果是:

int_field      str_field
3              var=3
于 2013-11-02T21:10:42.847 回答