I have a table with the fields products, preco1, preco2, preco3.
How do I show the lowest priced product?
SELECT produto, LEAST(preco1, preco2, preco3) FROM precos WHERE codigo=?
The least()
operator will select the smallest argument of a particular query .
With two or more arguments, returns the smallest argument (minimum value) ... If the arguments comprise a mixture of numbers and strings , they are compared as numbers.
To update melhorpreco
, just run the following query :
UPDATE precos SET melhorpreco = LEAST(preco1, preco2, preco3) WHERE codigo=?
Where the ?
were placed you should enter the registry code you want to make the change.
You can see this SQL Fiddle that demonstrates what you want too.