0

How can I make this work - gives me #1093 - You can't specify target table 'Orders' for update in FROM clause

Update Orders set flag = 0 where orders_id = (select orders_id from orders ORDER BY orders_id DESC LIMIT 1)
4

2 に答える 2

1

You need to wrap it in a subquery (creating a temporary table)

Update Orders 
set flag = 0 
where orders_id = 
    (
        SELECT x.orders_id
        FROM
        (
            select orders_id 
            from orders 
            ORDER BY orders_id DESC 
            LIMIT 1
        ) x
    )

or

Update Orders 
set flag = 0 
where orders_id = 
    (
        SELECT x.orders_id
        FROM
        (
            select MAX(orders_id) orders_id 
            from orders 
        ) x
    )
于 2012-10-16T14:04:29.350 に答える
0

it looks like you are trying up update the last order in the table? try

Update Orders set flag = 0 ORDER BY orders_id DESC LIMIT 1; 

Mysql doesn't support updating a table while using it in a subquery..

于 2012-10-16T14:05:28.167 に答える