【问题标题】:Group similar data from multiple tables and add result将多个表中的相似数据分组并添加结果
【发布时间】:2015-02-28 02:27:01
【问题描述】:

我希望能够显示客户拥有的商品数量和总价,并添加每个 itemid 的价格和总价。我无法锻炼如何做到这一点的逻辑。到目前为止,我可以显示客户拥有的所有项目,但有些项目是重复的。

$clientid = 24;

$query = "SELECT items.itemid, prices.priceid FROM items 
JOIN prices ON items.itemid = prices.itemid 
WHERE items.clientid = '$clientid'";


$result =  $db_connect -> query($query);
$row_cnt = $result -> num_rows; 

echo $joinrow_cnt."<br/><br/><br/>";

while($assocresult = mysqli_fetch_assoc($result)){
    $price = $assocresult['priceid'];
    $item = $assocresult['itemid'];
    $realprice = ($price/100)*80;        //after 80% discount
    echo "ItemId: ".$item." Price : " .$price." real price: " .$realprice."<br/>";  
}

到目前为止,我得到了:

ItemId: 1 Price : 100 real price: 80
ItemId: 3 Price : 25 real price: 20
ItemId: 1 Price : 100 real price: 80
ItemId: 5 Price : 200 real price: 160
ItemId: 3 Price : 25 real price: 20
ItemId: 1 Price : 100 real price: 80

查看 ItemId 1 和 3 是如何重复的。有很多这样的行,其中项目 ID 重复。

我希望输出是这样的:

ItemId: 1x3 Price: 100 real price = 80x3=240
ItemId: 3x2 Price: 25 real price = 20x2=50
ItemId: 5   Price: 200 real price: 160 //since this item is listed only once.

请帮忙。非常感谢!!

【问题讨论】:

  • sum(prices.priceid) and group by items.itemid and count(items.itemid) if you want to count the repetites

标签: php mysqli sum average


【解决方案1】:

检查解决方案如下:

$clientid = 24;

$query = "SELECT items.itemid, prices.priceid, count(items.itemid) as itemcnt FROM items
JOIN prices ON items.itemid = prices.itemid 
WHERE items.clientid = '$clientid' group by items.itemid";


$result =  $db_connect -> query($query);
$row_cnt = $result -> num_rows; 

echo $row_cnt."<br/><br/><br/>";

while($assocresult = mysqli_fetch_assoc($result)){
$price = $assocresult['priceid'];
$item = $assocresult['itemid'];
$itemcnt = $assocresult['itemcnt'];
$realprice = ($price/100)*80;        //after 80% discount
echo "ItemId: ".$item."x".$itemcnt." Price: " .$price." real price = " .$realprice."x".$itemcnt."=".($realprice*$itemcnt)."<br/>";
}

当您进入第 10 行时,我没有收到任何错误。您的代码中 $db_connect 的值是什么?

告诉我。谢谢!

【讨论】:

  • 谢谢.. 对于行数.. 我需要使用 $assoc['itemcnt'];
【解决方案2】:

您可以获取所有itemsprices 数据并将它们与WHERE 匹配,然后使用GROUP BY 将重复项收集为一行。使用COUNT(..),您可以获得每个项目的行数。

这应该可以解决问题:

SELECT
    COUNT(items.itemid) as cnt,
    items.itemid,
    prices.priceid
FROM items, prices
WHERE 
    items.itemid = prices.itemid
AND
    items.clientid = '$clientid'
GROUP BY items.itemid

【讨论】:

  • 对不起,我尝试了这个查询,但没有给出任何结果。我收到错误:尝试在第 10 行第 10 行中获取非对象的属性。第 10 行:$row_cnt = $result -> num_rows;我的问题是:code $myquery = "SELECT COUNT(items.itemid) as cnt, items.itemid, prices.priceid FROM items, prices WHERE items.itemid = prices.itemid AND items.clientid = '$clientid' GROUP BY items.itemid"; $result = $db_connect -> 查询($myquery); $row_cnt = $result -> num_rows; echo $row_cnt;code 请问我做错了什么。非常感谢您帮助我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-05
  • 2012-03-04
  • 1970-01-01
  • 1970-01-01
  • 2021-12-24
相关资源
最近更新 更多