1

这是我的小提琴。我的查询:

update foos
set foo_id = ((SELECT max_foo_id FROM (SELECT MAX(foo_id) AS max_foo_id FROM foos) AS temp_foos) + 1)
where foo_id is null; 

为 foo_ids生成值1, 2, 2, 2(子查询只运行一次?)但我希望它给我1, 2, 3, 4

4

1 回答 1

2

这个怎么样?

UPDATE  foos a
        INNER JOIN
        (
            SELECT  a.bar_ID,
                    @rn := @rn + 1 row_Num
            FROM    foos a,(SELECT @rn := (SELECT MAX(foo_ID) FROM foos)) b
            WHERE   a.foo_ID IS NULL
        ) b ON a.bar_ID = b.bar_ID
SET     a.foo_id = b.row_Num

更新 1

于 2013-05-02T01:21:13.053 回答