我的桌子上有两个独特的约束。
CREATE TABLE `targets` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`client` SMALLINT(6) UNSIGNED NOT NULL,
-- some more fields here
`order_column` INT(10) NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `client_order_column` (`client`, `order_column`)
)
现在我想更新它,order_column
将包含增量整数,从0
开始,每个client
值。我试图这样做:
replace into targets (
`id`,
`client` ,
`order_column`
)
(select
`id`,
`client` ,
0 as order_column from targets
)
on duplicate key update
order_column = order_column + 1
但在最后一行获得syntax error
。我无法意识到,有什么不对,所以,请帮助我。
答案 0 :(得分:0)
INSERT INTO targets (id, client, order_column)
SELECT a.id, a.client , 0 as order_column
FROM targets a
ON DUPLICATE KEY
UPDATE order_column = a.order_column + 1