3
所以,我有如下表:MySQL的INSERT ... ON DUPLICATE KEY沒有更新的表沒有錯誤或警告
mysql> show create table user_api_skills \G
*************************** 1. row ***************************
Table: user_api_skills
Create Table: CREATE TABLE `user_api_skills` (
`characterID` int(11) NOT NULL,
`typeID` int(11) NOT NULL,
`level` enum('0','1','2','3','4','5') NOT NULL DEFAULT '0',
`skillpoints` int(11) NOT NULL,
`currentTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`characterID`,`typeID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql>
並在表中的一行,我正在嘗試插入/更新:
mysql> SELECT * FROM `user_api_skills` WHERE `characterID` =93192782 AND `typeID` =3359;
+-------------+--------+-------+-------------+---------------------+
| characterID | typeID | level | skillpoints | currentTime |
+-------------+--------+-------+-------------+---------------------+
| 93192782 | 3359 | 3 | 135765 | 2013-09-30 16:58:35 |
+-------------+--------+-------+-------------+---------------------+
1 row in set (0.00 sec)
我認爲是正確形成我的查詢,並在執行時會不會引發任何錯誤或警告:
mysql> INSERT INTO user_api_skills (characterID,typeID,level,skillpoints)
VALUES (93192782,3359,4,135765) ON DUPLICATE KEY UPDATE level=4,
skillpoints=135765,currentTime=NOW();
Query OK, 2 rows affected (0.22 sec)
我得到2行更新(因爲我會從DUP更新插入預期)
mysql> SELECT * FROM `user_api_skills` WHERE `characterID` =93192782 AND `typeID` =3359;
+-------------+--------+-------+-------------+---------------------+
| characterID | typeID | level | skillpoints | currentTime |
+-------------+--------+-------+-------------+---------------------+
| 93192782 | 3359 | 3 | 135765 | 2013-09-30 16:59:13 |
+-------------+--------+-------+-------------+---------------------+
1 row in set (0.00 sec)
mysql>
,但該行本身只改變單一值(currentTime的)。任何人都可以解釋爲什麼其他兩個字段沒有更新?
技能點是相同的值? – Tommassiov
對不起,我自己解決了這個問題。級別字段是ENUM,查詢將新值指定爲數字。將查詢更新爲以下內容會產生預期的結果。 INSERT INTO user_api_skills(characterID,typeID,level,skillpoints)VALUE(93192782,3359,4,135765)ON DUPLICATE KEY UPDATE level ='4',skillpoints = 135765,currentTime = NOW();' – ModulusJoe
Post作爲答案並接受你自己的答案。 – Mihai