简体   繁体   English

如何从SELECT语句获取LEAST()值?

[英]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. 显然我不能将LEAST()与其中的SELECT语句一起使用。

In MySQL, LEAST() is expecting a number of arguments, and returns the smallest one of those. 在MySQL中, LEAST()需要多个参数,并返回其中最小的一个。

What you are getting with your SELECT is however a result set, and therefore I think you are looking for MIN() . 但是,通过SELECT获得的是结果集,因此我认为您正在寻找MIN() Exchange your LEAST() with this and it should do the trick. 与此交换您的LEAST(),它应该可以解决问题。

不是mysql的专家,但是您可以使用以下代码来解决此问题: SELECT Least(valor_total) FROM tbl_pedidos_cotacaos_produtos WHERE produto_id = ".$produto->itens[$t]->pedido_id. "而是在选择命令上使用显式最小函数:)

使用MIN()函数可找到多行中的最小值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM