Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 查询集中的最大值_Php_Mysql_Max_Opencart - Fatal编程技术网

Php 查询集中的最大值

Php 查询集中的最大值,php,mysql,max,opencart,Php,Mysql,Max,Opencart,我有以下代码 $this->db->query("UPDATE customer_product_owned SET quantity = (max(0, (quantity - " . (int)$order_product_upgrade['quantity'] . "))) WHERE customer_id = '" . (int)$order_info['customer_id'] . "'"); 我想从quantity字段的值中减去php变量。但是,如果该值

我有以下代码

$this->db->query("UPDATE customer_product_owned
   SET quantity = (max(0, (quantity - " . (int)$order_product_upgrade['quantity'] . ")))
   WHERE customer_id = '" . (int)$order_info['customer_id'] . "'");
我想从quantity字段的值中减去php变量。但是,如果该值小于0,我希望该值为0

我做错了什么?max可以在“集合”中使用吗?我可以事先通过一个额外的查询来实现这一点,首先找出数量的值,但是有没有办法在同一个查询中实现这一点

  • MAX
    是一个聚合函数。您需要
    最大值(0,…)
  • 不要用引号括起数字。只需按原样指定它们,如:

    WHERE customer_id = " . (int)$order_info['customer_id']
    
  • 为什么
    (max(0,(quantity-”(int)$order\u product\u upgrade['quantity']))
    ,而不仅仅是
    quantity=quantity-”(int)$order\u product\u upgrade['quantity'])。
    @dbf:“但是,如果该值小于0,我希望该值为0。”