我正在尝试在MySQL中插入行,如果同一个表中已存在nick
的行。这是我正在尝试的查询:
INSERT IGNORE INTO gold_log (nick, amount, stream_online, modification_type, dt)
SELECT DISTINCT nick, 0, 0, 253, DATE_FORMAT(NOW(), '%Y-01-01 00:00:00')
FROM gold_log WHERE nick='PrestonConnors';
我得到结果:
Query OK, 2243 rows affected, 2 warnings (0.24 sec)
Records: 2243 Duplicates: 0 Warnings: 2
当我独立运行 SELECT 语句时,它只返回一个结果(这是我所期望的):
mysql> SELECT DISTINCT nick, 0, 0, 253, DATE_FORMAT(NOW(), '%Y-01-01 00:00:00') FROM gold_log WHERE nick='PrestonConnors';
+----------------+---+---+-----+-----------------------------------------+
| nick | 0 | 0 | 253 | DATE_FORMAT(NOW(), '%Y-01-01 00:00:00') |
+----------------+---+---+-----+-----------------------------------------+
| PrestonConnors | 0 | 0 | 253 | 2015-01-01 00:00:00 |
+----------------+---+---+-----+-----------------------------------------+
1 row in set (0.01 sec)
有人可以帮助我将 INSERT IGNORE INTO 语句形成一个只会 INSERT 一行进入表中并解释我的查询有什么问题吗?
这是表格的布局:
mysql> describe gold_log;
+-------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| nick | char(25) | NO | PRI | NULL | |
| amount | decimal(10,4) | YES | MUL | NULL | |
| stream_online | tinyint(1) | NO | MUL | NULL | |
| modification_type | tinyint(3) unsigned | NO | MUL | NULL | |
| dt | datetime | NO | PRI | NULL | |
+-------------------+---------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
以下是警告:
mysql> SHOW warnings;
+-------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note | 1592 | Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. INSERT IGNORE... SELECT is unsafe because the order in which rows are retrieved by the SELECT determines which (if any) rows are ignored. This order cannot be predicted and may differ on master and the slave. |
| Note | 1592 | Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. Statements writing to a table with an auto-increment column after selecting from another table are unsafe because the order in which rows are retrieved determines what (if any) rows will be written. This order cannot be predicted and may differ on master and the slave. |
+-------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
答案 0 :(得分:-1)
简单来说,这是MySQL中的一个错误(我在mysql5.5.16中找到它);但是当我更新到(5.6.24)时:
mysql> status
--------------
D:\xampp\mysql\bin\mysql.exe Ver 14.14 Distrib 5.6.24, for Win32 (x86)
它运作正常。
见:
http://bugs.mysql.com/bug.php?id=58637
http://bugs.mysql.com/bug.php?id=72921