使用之类的语句IF
属于像一个存储过程的代码块中。你将无法在mysql提示符下执行它。
如果你只是想插入一个随机值,这是以前没有的你也可以做到这一点通过
mysql> create table nums(num int, unique key(num));
Query OK, 0 rows affected (0.05 sec)
mysql> insert ignore into nums >select round(rand()*9999);>
Query OK, 1 row affected (0.01 >sec)>
Records: 1 Duplicates: 0 Warn>ings>: 0>
mysql> insert ignore into nums select round(rand()*9999);
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert ignore into nums select round(rand()*9999);
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert ignore into nums select round(rand()*9999);
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from nums;
+------+
| num |
+------+
| 5268 |
| 9075 |
| 9114 |
| 9768 |
+------+
4 rows in set (0.00 sec)
mysql>
随着insert ignore
,如果它已经存在,它不会插入一行。
的MySQL或Oracle?他们完全不同。 – Mat 2013-03-18 12:29:40
IF语句只适用于TSQL和PL/SQL。如果你只是执行查询,那是行不通的。您必须将该逻辑放入执行代码中。 – rbedger 2013-03-18 12:30:01
我该怎么做?我正在使用与当前版本的WampServer和XAMPP一起使用的MySQL版本。 – user2182300 2013-03-18 13:18:51