在存储过程中先做一下select count(*) into @cnt from yourTable where xxxx; if @cnt=0 then insert into yorrTble ....
select count(*) into @cnt from relationtable where tableName='mytable' and fieldName='name'; if @cnt=0 then insert into relationtable VALUES ('a','b','c','d')
insert into relationtable select distinct 'a','b','c','d' from relationtable where tableName='mytable' and fieldName='name';你的语句要在SP中用
这个好象也不行呀, 如果表里没有数据的话,不能insert
那当然,你增加一条比较特殊的记录,INSERT 结束后删除
直接运行的话,用下面这个语句。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>
if @cnt=0 then
insert into yorrTble ....
select count(*) into @cnt from relationtable where tableName='mytable' and fieldName='name';
if @cnt=0 then
insert into relationtable VALUES ('a','b','c','d')
select distinct 'a','b','c','d' from relationtable where tableName='mytable' and fieldName='name';你的语句要在SP中用
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>