如何添加重复产品条目的数量。如果产品数量少于 8 return 它
How to Add the Quantity of Duplicate Products entries. If the Product Quantity is Less than 8 return it
在我的API。我有一个产品 table 具有属性
product_id, category_id, item_id, size_id, brand_id, product_price,
product_quantity, location_id, product_manufacture, product_expire,
created_at
我正在return查询数量小于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"
}
]
}
这里我在客户端预览数据
这是return获取产品信息的代码。
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;
}
但是我在数据库中有多次相同的产品条目,因此之间的差异是产品制造日期。
这里我想return比较后的信息,如果category_id,item_id,size_id
和brand_id
相同,就把每个产品的数量加上,然后只有数量是不到8个,return吧。
我该怎么做?
- 谢谢。
问题已通过在查询中使用 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
在我的API。我有一个产品 table 具有属性
product_id, category_id, item_id, size_id, brand_id, product_price, product_quantity, location_id, product_manufacture, product_expire, created_at
我正在return查询数量小于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"
}
]
}
这里我在客户端预览数据
这是return获取产品信息的代码。
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;
}
但是我在数据库中有多次相同的产品条目,因此之间的差异是产品制造日期。
这里我想return比较后的信息,如果category_id,item_id,size_id
和brand_id
相同,就把每个产品的数量加上,然后只有数量是不到8个,return吧。
我该怎么做?
- 谢谢。
问题已通过在查询中使用 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