如果select语句为true,则插入mysql

时间:2014-06-11 06:44:52

标签: mysql sql

我有一个MySQL查询:

select count(id) as tot from user where sponsor_id=10006 and exp > now();

上述查询返回1为tot。

如果上述查询返回的值超过1,则应在表中插入记录。

1 个答案:

答案 0 :(得分:0)

你可以这样写:

insert into log
select 'there are more than one IDs'
from dual -- this is just a dummy table in mysql
where 1 <> (select count(id) as tot 
              from user 
             where sponsor_id=10006
               and exp > now()
           )

您可以查看this demo on SQLFiddle(不包含exp&gt; now()部分)。