MySQL: How to do a conditional update?

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2010-04-04T21:52:43Z Indexed on 2010/04/04 21:53 UTC
Read the original article Hit count: 202

Filed under:
|
|
|

Hi,

I'm trying to a create an update statement along the following lines:

TABLE car: id | owner_id | type | status

An owner can have multiple cars.

UPDATE car c 
   SET c.type = 1 
     WHERE c.owner_id IN ($ids) 
     AND c.status = [1 IF IT EXISTS, ELSE 0]

$ids is reasonably small (under 50 values).

It seems simple but I can't seem to get my head around it because I can't use a SELECT subquery with an UPDATE to the same table.

Anyone?

Thanks

© Stack Overflow or respective owner

Related posts about mysql

Related posts about update