汇总数据库表中的结果

时间:2019-03-30 10:36:57

标签: php mysqli

我有一张这样的桌子:

id  |  name  | quantity | price   
================================
1   | milk   |     3    |   3.5
-------------------------------
2   | jam    |     2    |   1.45
--------------------------------
3   | jam    |     1    |   1.45
--------------------------------
4   | milk   |     6    |   3.5
-------------------------------

例如,我需要打印汇总结果:

牛奶-数量:9-价格:3.5-总和:31,5

果酱-数量:3-价格:1.45-总和:4.35

不喜欢这个:

牛奶-数量:3-价格:3.5-总和:10.5

果酱-数量:2-价格:1.45-总和:2.9

果酱-数量:1-价格:3.5-总和:1.45

牛奶-数量:6-价格:3.5-总和:21

这是我的代码,但没有给我我想要的:


  $conn=mysqli_connect('localhost','root','','shop') or die ("Fail");
  $sql="select *  from products";
  $result=mysqli_query($conn, $sql);
  while ($role=mysqli_fetch_array($result)){
        extract ($role);
         if ($quantity>0) {
    echo " $name - Price: $price - Quantity: $quantity -  Sum is: ".round($quantity*$price,2)."<br>";
          }  
           else {
               $m[$i]="$name - Price: $price <br>";
               $i++;
           }
  }

请帮我解决。谢谢:)

4 个答案:

答案 0 :(得分:0)

您可以通过mysql查询来做到这一点。

select *,sum(quantity) as total_quantity, round(sum(quantity*price),2) as total_price from products group by name

答案 1 :(得分:0)

您可以尝试像这样修改代码:

$conn=mysqli_connect('localhost:3310','root','','shop') or die ("Fail");
$sql="select name,sum(quantity) quantity,price,sum(quantity)*price sumAmount  from products group by name,price";
$uspv=mysqli_query($conn, $sql);
$i=1;

while ($uspm=mysqli_fetch_array($uspv)){
    extract ($uspm);
    if ($quantity>0) {
        echo " $name - Price: $price - Quantity: $quantity -  Sum is: $sumAmount <br>";
    }  
    else {
        $m[$i]="$name - Price: $price <br>";
        $i++;
    }
} 

答案 2 :(得分:0)

您可以使用CONCAT,GROUP BY,ORDER BY来完成此操作,而无需再使用PHP逻辑

SELECT 
CONCAT(
 name,"- Quantity :- ",
SUM(`quantity`),"- Price :- ",
price,"- Sum :-", 
SUM(quantity*price)) AS result
FROM grocery GROUP BY name,price ORDER BY name ASC

结果:enter image description here-

Phpmyadmin:-

enter image description here

PHP的工作代码:-

$conn = mysqli_connect('localhost','root','','DBNAME') or die ("Fail");
$sql  = 'SELECT CONCAT( name,"- Quantity :- ",SUM(`quantity`),"- Price :- ",price,"- Sum :-", SUM(quantity*price))  AS result FROM grocery GROUP BY name,price ORDER BY name ASC';

$result = $conn->query($sql);
$rows   = array();

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $rows[] = $row;
    }
}

使用数组打印:-

echo '<pre>';
print_r($rows);

结果:-

Array
(
 [0] => Array
    (
        [result] => jam- Quantity :- 3- Price :- 1.45- Sum :-4.35
    )

 [1] => Array
    (
        [result] => milk- Quantity :- 9- Price :- 3.50- Sum :-31.50
    )

)

答案 3 :(得分:0)

您能否仅使用SQL解决此问题

use2

结果:

select 
  name, 
  sum(quantity) quantity , price, 
  ROUND(sum(price*quantity),2) as Sum
from 
   products
group by name;

如果您想同时获得结果:

name                 quantity                          price        Sum                 
-------------------- --------------------------------- ------------ ------------------- 
jam                  3                                 1.45         4.35                
milk                 9                                 3.5          31.5                

结果:

select 
   concat(a. name, ' - quantity: ', a.quantity, ' - price: ',  a.price, ' - Sum: ', a.Sum ) resultado
from (
select 
   name, 
   sum(quantity) quantity , price, 
   ROUND(sum(price*quantity),2) as Sum
from 
   products
group by name ) a;

之后,您可以使用PHP代码将结果放入页面中。

我测试了这些sql代码。

希望这会有所帮助