Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.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 如何添加重复产品分录的数量。如果产品数量少于8,则将其退回_Php_Mysql_Prepared Statement - Fatal编程技术网

Php 如何添加重复产品分录的数量。如果产品数量少于8,则将其退回

Php 如何添加重复产品分录的数量。如果产品数量少于8,则将其退回,php,mysql,prepared-statement,Php,Mysql,Prepared Statement,在我的API中。我有一个带有属性的产品表 产品标识、类别标识、项目标识、尺寸标识、品牌标识、产品价格、, 产品数量、位置id、产品制造、产品过期、, 创建于 我将返回数量小于8的产品信息响应 { "error": false, "message": "Products Found", "products": [ { "productId&q

在我的API中。我有一个带有属性的产品表

产品标识、类别标识、项目标识、尺寸标识、品牌标识、产品价格、, 产品数量、位置id、产品制造、产品过期、, 创建于

我将返回数量小于8的产品信息响应

{
    "error": false,
    "message": "Products Found",
    "products": [
        {
            "productId": 6,
            "productCategory": "CAPSULE",
            "productName": "PYTHON",
            "productSize": "100 PILLS",
            "productBrand": "FHC",
            "productPrice": 401,
            "productQuantity": 5,
            "productLocation": "A1",
            "productManufacture": "2016-07",
            "productExpire": "2029-06"
        },
        {
            "productId": 5,
            "productCategory": "CAPSULE",
            "productName": "ANDROID",
            "productSize": "100 PILLS",
            "productBrand": "FHC",
            "productPrice": 401,
            "productQuantity": 5,
            "productLocation": "A1",
            "productManufacture": "2016-07",
            "productExpire": "2025-06"
        },
        {
            "productId": 4,
            "productCategory": "CAPSULE",
            "productName": "PYTHON",
            "productSize": "100 PILLS",
            "productBrand": "FHC",
            "productPrice": 401,
            "productQuantity": 6,
            "productLocation": "A1",
            "productManufacture": "2016-07",
            "productExpire": "2022-06"
        }
    ]
}
这里我在客户端预览数据

这是返回产品信息的代码

function getNoticeProducts()
{
    $products = array();
    $productss = array();
    $productsss = array();
    $query = "
SELECT product_id
     , category_id
     , item_id
     , size_id
     , brand_id
     , product_price
     , product_quantity
     , location_id
     , product_manufacture
     , product_expire 
  FROM products 
 WHERE product_expire >= DATE_ADD(CURDATE(), INTERVAL 1 DAY) 
 ORDER 
    by product_expire DESC
";
    $stmt = $this->con->prepare($query);
    $stmt->execute();
    $stmt->bind_result($productId,$categoryId,$itemId,$sizeId,$brandId,$productPrice,$productQuantity,$locationId,$productManufacture,$productExpire);
    while ($stmt->fetch())
    {
        $product['productId']           = $productId;
        $product['categoryId']          = $categoryId;
        $product['itemId']              = $itemId;
        $product['sizeId']              = $sizeId;
        $product['brandId']             = $brandId;
        $product['productPrice']        = $productPrice;
        $product['productQuantity']     = $productQuantity;
        $product['locationId']          = $locationId;
        $product['productManufacture']  = $productManufacture;
        $product['productExpire']       = $productExpire;
        array_push($products, $product);
    }
    foreach ($products as  $product)
    {
        $salesQuantity = $this->getAllSalesQuantityOfProudctById($product['productId']);
        $sellerSalesQuantity = $this->getAllSellerSalesQuantityOfProudctById($product['productId']);
        if ($product['productQuantity']-$salesQuantity-$sellerSalesQuantity<8)
        {
            $pro['productId']               = $product['productId'];
            $pro['productCategory']         = $this->getCategoryById($product['categoryId']);
            $pro['productName']             = $this->getProductNameByItemId($product['itemId']);
            $pro['productSize']             = $this->getSizeById($product['sizeId']);
            $pro['productBrand']            = $this->getBrandById($product['brandId']);
            $pro['productPrice']            = $product['productPrice'];
            $pro['productQuantity']         = $product['productQuantity']-$this->getSellQuantityByProductId($pro['productId'])-$this->getAllSellerSalesQuantityOfProudctById($pro['productId']);
            $pro['productLocation']         = $this->getLocationById($product['locationId']);
            $pro['productManufacture']      = substr($product['productManufacture'], 0, 7);
            $pro['productExpire']           = substr($product['productExpire'], 0, 7);
            array_push($productss, $pro);
        }
    }
    return $productss;
}
函数getNoticeProducts() { $products=array(); $productss=array(); $productsss=array(); $query=” 选择产品标识 ,类别识别号 ,项目编号 ,尺码 ,品牌识别码 ,产品价格 ,产品数量 ,位置_id ,产品制造 ,产品过期 来自产品 其中产品过期>=日期添加(CURDATE(),间隔1天) 命令 按产品说明 "; $stmt=$this->con->prepare($query); $stmt->execute(); $stmt->bind_result($productId、$categoryId、$itemId、$sizeId、$brandId、$productPrice、$productQuantity、$locationId、$productManufacture、$productExpire); 而($stmt->fetch()) { $product['productId']=$productId; $product['categoryId']=$categoryId; $product['itemId']=$itemId; $product['sizeId']=$sizeId; $product['brandId']=$brandId; $product['productPrice']=$productPrice; $product['PRODUCTQUOTE']=$PRODUCTQUOTE; $product['locationId']=$locationId; $product['PRODUCTMANAGE']=$PRODUCTMANAGE; $product['productExpire']=$productExpire; 阵列推送($products,$product); } foreach($products as$product) { $salesQuantity=$this->getAllsalesQuantityofProudCbyId($product['productId'); $sellerSalesQuantity=$this->GetAllSellerSalesQuantityOfProdctById($product['productId']); if($product['PRODUCTQUOTE']-$SALESSEQUOTITYGetCategoryById($product['categoryId']); $pro['productName']=$this->getProductNameByItemId($product['itemId']); $pro['productSize']=$this->getSizeById($product['sizeId']); $pro['productBrand']=$this->getBrandById($product['brandId']); $pro['productPrice']=$product['productPrice']; $pro['productQuantity']=$product['productQuantity']-$this->getSellQuantityByProductId($pro['productId'])-$this->GetAllSellerSalesQuantityOfProdctById($pro['productId']); $pro['productLocation']=$this->getLocationById($product['locationId']); $pro['productManufacture']=substr($product['productManufacture'],0,7); $pro['productExpire']=substr($product['productExpire'],0,7); 阵列推送($productss,$pro); } } 返回$productss; } 但我在数据库中不止一次地有相同的产品条目,所以它们之间的差异是产品制造日期

这里我想在比较后返回信息,比如,如果
category\u id、item\u id、size\u id
brand\u id
相同,则取每个产品的数量添加它,然后仅当数量小于8时,才返回它

我该怎么做

  • 谢谢

通过在查询中使用
SUM()
groupby
解决了该问题

SELECT
    product_id,
    category_id,
    item_id,
    size_id,
    brand_id,
    product_price,
    SUM(product_quantity),
    location_id,
    product_manufacture,
    product_expire
FROM products
WHERE
    product_expire >= DATE_ADD(CURDATE(), INTERVAL 1 DAY)
GROUP BY (item_id),(brand_id),(category_id),(size_id)
ORDER by product_expire DESC

查询是如何生成的?通常您将其与sum和group by结合使用,我建议不要使用n+1查询,因为您可以通过产品idThankyou@KEVIN连接它们。这返回一个任意的产品id。此外,这表明您使用的是MySQLThankyou的一个相当旧的版本,用于您的建议@草莓。您可以我会提供任何可以提高我技能的参考资料,看看旧版本和新版本的查询结果。或者它们之间有什么不同。我只能让你参考我的原始评论。