0

我现在没有地方测试这个,我希望有人知道,所以我不必等到明天才能找到......

   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()  

是的?

或者不,因为PropIDandUpdateOn不能被on dup部件访问,而不是在值列表中......?

我尝试使用 SQLFiddle,但它告诉我没有 DDL 或 DML 语句,只有选择。


所以我测试了小提琴......

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)

这不是预期的输出......

所以......我猜这两件事真的不做同样的事情,我不需要按照我最初建议的方式重新编写代码。 它是有效的语法,但不是正确的结果。

只是为了澄清(但我相信你可以从最初的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

谢谢您的帮助!

4

1 回答 1

0

它是有效的 SQL。也就是说,以上两种INSERT ... ON DUPLICATE KEY UPDATE说法会产生相同的效果。

这可以通过 SQLFiddle 显示,只是插入必须是 DDL 的一部分。

http://sqlfiddle.com/#!2/56579/1/0

如果那ItemID = VALUES(ItemID)是重复键,那也是毫无意义的。

于 2013-08-19T22:50:10.537 回答