0

我有一个对两个字段具有唯一约束的表。

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在最后一行。我无法意识到,出了什么问题,所以,请帮助我。

顺便说一句:有没有更好的方法来达到预期的结果?

4

1 回答 1

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
于 2013-02-26T11:41:44.707 回答