微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何添加重复产品条目的数量如果产品数量小于 8,则返回它

如何解决如何添加重复产品条目的数量如果产品数量小于 8,则返回它

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

product_id、category_id、item_id、size_id、brand_id、product_price、 product_quantity,location_id,product_manufacture,product_expire,created_at

enter image description here

enter image description here

并且我返回的是数量小于 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,"productName": "ANDROID","productExpire": "2025-06"
        },{
            "productId": 4,"productQuantity": 6,"productExpire": "2022-06"
        }
    ]
}

这里我在客户端预览数据。

enter image description here

这是返回产品信息的代码

function getNoticeProducts()
{
    $products = array();
    $productss = array();
    $productsss = array();
    $query = "
SELECT product_id,category_id,item_id,size_id,brand_id,product_price,product_quantity,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'],7);
            $pro['productExpire']           = substr($product['productExpire'],7);
            array_push($productss,$pro);
        }
    }
    return $productss;
}

但我在数据库中有多次相同的产品条目,因此不同的是产品制造日期。

这里我想返回比较like后的信息,如果category_id,size_idbrand_id相同,把每个产品的数量加起来,然后只有数量小于8,才返回.

enter image description here

我该怎么做?

  • 谢谢。

解决方法

问题已通过在查询中使用 SUM()group by 解决。

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。