How do I search for the smallest value between the columns? [closed]

-4

I have a table with the fields products, preco1, preco2, preco3.

How do I show the lowest priced product?

    
asked by anonymous 29.08.2017 / 15:20

1 answer

1
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=?

Notes

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.

    
29.08.2017 / 15:49