判断mytabl表里是否有这条记录,没有的话,insert!有的话什么都不做

解决方案 »

  1.   

    在存储过程中先做一下select count(*) into @cnt from yourTable where xxxx;
    if @cnt=0 then
      insert into yorrTble ....
      

  2.   


    select count(*) into @cnt from relationtable where tableName='mytable' and fieldName='name'; 
    if @cnt=0 then 
      insert into relationtable VALUES ('a','b','c','d') 
      

  3.   

     insert into relationtable
    select distinct 'a','b','c','d' from relationtable where tableName='mytable' and fieldName='name';你的语句要在SP中用
      

  4.   

    这个好象也不行呀, 如果表里没有数据的话,不能insert
      

  5.   

    那当然,你增加一条比较特殊的记录,INSERT 结束后删除
      

  6.   

    直接运行的话,用下面这个语句。insert into test 
    select 1,2 from dual where not exists (select id from test where id=1);测试如下。
    mysql> create table test(id int primary key,c1 int);
    Query OK, 0 rows affected (0.08 sec)mysql> insert into test
        -> select 1,2 from dual where not exists (select id from test where id=1);
    Query OK, 1 row affected (0.05 sec)
    Records: 1  Duplicates: 0  Warnings: 0mysql> insert into test
        -> select 1,2 from dual where not exists (select id from test where id=1);
    Query OK, 0 rows affected (0.00 sec)
    Records: 0  Duplicates: 0  Warnings: 0mysql>
    mysql> select * from test;
    +----+------+
    | id | c1   |
    +----+------+
    |  1 |    2 |
    +----+------+
    1 row in set (0.00 sec)mysql>