我現在沒有地方來測試這個權利,我希望有人知道,所以我不必等到明天找出....這是否有效的「重複鍵」語法?
insert into item_properties (ItemID, PropID, Value, UpdateOn) values
(538, 25, 'some description stuff goes here', unix_timestamp()),
(541, 25, 'some description stuff goes here', unix_timestamp()),
(1276, 25, 'some description stuff goes here', unix_timestamp()),
(1319, 25, 'some description stuff goes here', unix_timestamp())
on duplicate key update
ItemID = values(ItemID),
PropID = values(PropID),
Value = values(Value),
UpdateOn = values(UpdateOn)
可以在重新寫爲:
insert into item_properties (ItemID, Value) values
(538, 'some description stuff goes here'),
(541, 'some description stuff goes here'),
(1276, 'some description stuff goes here'),
(1319, 'some description stuff goes here')
on duplicate key update
ItemID = values(ItemID),
Value = values(Value),
PropID = 25,
UpdateOn = unix_timestamp()
是嗎?
或不,因爲PropID
和UpdateOn
不能被on dup
零件訪問而不在值列表中...?
我試圖SQLFiddle,但它告訴我一些關於沒有DDL或DML語句,只有選擇。
所以我測試的filddle ...
insert into item_properties (ItemID, Value) values
(538, 'some description stuff goes here'),
(538, 'some other description stuff goes here'),
(1276, 'some description stuff goes here'),
(1319, 'some description stuff goes here')
on duplicate key update
ItemID = values(ItemID),
PropID = 26,
Value = values(Value),
UpdateOn = unix_timestamp()
變爲:
ITEMID PROPID VALUE UPDATEON
538 26 some other description stuff goes here 1376952345
1276 (null) some description stuff goes here (null)
1319 (null) some description stuff goes here (null)
這是不想要的輸出...
所以。 ..我猜這兩件事真的是不要做s但我不需要按照我最初的建議重新編寫代碼。 它是有效的語法,但不正確的結果。
只是爲了澄清(但我敢肯定,你可以通過初始on duplicate key
聲明說),這是輸出我應該結束了...
ITEMID PROPID VALUE UPDATEON
538 26 some other description stuff goes here 1376952345
1276 26 some description stuff goes here 1376952345
1319 26 some description stuff goes here 1376952345
感謝您的幫助!
「但它告訴我一些關於沒有DDL或DML語句的東西」---然後以某種方式修復它。或者分享**確切的**錯誤信息。 – zerkms
@zerkms - ItemID不會更新爲相同的值...?這幾乎是我得到的確切的錯誤,而且與聲明的有效性無關(這只是我說我爲什麼現在不能測試自己)......顯然只是一個問題,將陳述放入測試運行。 –
SQLFiddle錯誤:在MySQL的查詢面板中不允許使用DDL和DML語句;只允許SELECT語句。把DDL和DML放到模式面板中(通過將insert/on-dup-key語句移動到模式面板並在SQL面板中選擇* select * from table)來解決這個問題) –