samedi 27 juin 2015

Using MIN() in SET statement MySQL

I am using MySQL. Lets call a table that I have as Inventory which looks is below:

+----+--------+--------+-------------+----------+ | ID | Price1 | Price2 | TargetPrice | Quantity | +----+--------+--------+-------------+----------+ | 1 | 12 | 1 | | 0 | | 2 | 3 | 3 | 3 | 2 | | 3 | | 4 | | 0 | | 4 | 2 | 2 | 2 | 2 | | 5 | 5 | 45 | 5 | 1 | +----+--------+--------+-------------+----------+

Now, I need to update the TargetPrice to minimum of Price1 and Price2 for any row whose Quantity is 0

I have tried:

UPDATE Inventory SET TargetPrice= MIN(Price1,Price2) WHERE Quantity >0

However, MySQL complains about the usage of MIN() function. I know it is expecting MIN() to work on the data contained inside column, rather than taking MIN() of two columns of a specified row.

Anyway to achieve this other than cursors?

Aucun commentaire:

Enregistrer un commentaire