开发者

MySQL update unique index

Is there a way to say U开发者_开发百科PDATE items SET qty=10 WHERE **unique key** instead of saying UPDATE items SET qty=10 WHERE userID=1 AND listID=10 and itemID=100 on the following table?

CREATE TABLE IF NOT EXISTS `items` (
  `userID` int(20) NOT NULL,
  `listID` int(20) NOT NULL,
  `itemID` int(20) NOT NULL,
  `qty` int(10) NOT NULL,
  UNIQUE KEY `unique` (`userID`,`listID`,`itemID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


I'm not 100% sure what you're asking, but if you want to know if you can name the unique index in the WHERE clause and supply some kind of single combined value to the find the row you want, the answer is no. If you want to use the index, you must specify the three separate column values as in your example and let MySQL figure out whether that is the best index to use (in this case, the answer will be yes).


The only way I know is:

UPDATE items
SET qty=10
WHERE (userID, listID, itemID) = (1, 10, 100)

Is that what you seek?

The syntax is equivalent to the one using AND, just a bit more compact. It doesn't guarantee that the index will be used although that is probable using either this or your syntax.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜