views:

34

answers:

1

I want to update a Column in a Table, based on the minimum of another column of the same table.

eg.

JobPositonId | JobPositonName | JobDescriptionId | ContactId
1            | 1              | 1                | 1
2            | 1              | 1                | 0
3            | 1              | 1                | 0

I want to update ContactId to be 1, if it is 0 and where JobPositionId is the lowest.

+1  A: 

I think this should work:

update jobTable
   set contactid = 1
 where jobPostitionId in (select pos from (select min(jobPositionId) as pos from jobTable where contactId = 0) as subtable);

It's kind of a hack similar to what's described here (http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/).

Dante617
AWESOME, thanks alot for that!!!
Robert de Klerk
Did it work for what you needed? (It's always tough to know when not having the database set up to test against locally.)
Dante617