Ericson Willians
Ericson Willians

Reputation: 7845

How can I get the LEAST() value from a SELECT statement?

Here's my query:

"UPDATE tbl_pedidos_cotacaos_produtos tb1 LEFT JOIN 
tbl_pedidos_produtos tb2 ON tb1.produto_id = tb2.id SET 
tb1.status = CASE WHEN tb1.valor_total = 
SELECT LEAST(SELECT valor_total FROM tbl_pedidos_cotacaos_produtos WHERE
produto_id = ".$produto->itens[$t]->pedido_id.") THEN 5 ELSE 4 WHERE pedido_id = ".$produto->itens[$t]->pedido_id

Error:

[19-Dec-2015 05:37:48 America/Sao_Paulo] You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT LEAST(SELECT valor_total FROM tbl_pedidos_cotacaos_produtos WHERE produto' at line 1

Apparently I can't use LEAST() with a SELECT statement inside of it.

Upvotes: 0

Views: 66

Answers (3)

ArSeN
ArSeN

Reputation: 5248

In MySQL, LEAST() is expecting a number of arguments, and returns the smallest one of those.

What you are getting with your SELECT is however a result set, and therefore I think you are looking for MIN(). Exchange your LEAST() with this and it should do the trick.

Upvotes: 3

Zebra North
Zebra North

Reputation: 11482

Use the MIN() function to find the smallest value across multiple rows.

... total = (SELECT MIN(valor_total) FROM ...)

Upvotes: 1

vignesh
vignesh

Reputation: 83

Not expert in mysql but you can use this code, to resolve this SELECT Least(valor_total) FROM tbl_pedidos_cotacaos_produtos WHERE produto_id = ".$produto->itens[$t]->pedido_id. " instead explicit Least function on a select command :)

Upvotes: 1

Related Questions