drwxg62286 2018-11-01 22:18
浏览 44
已采纳

一个查询的多个求和查询

I would like to make a collection according to some fields in the product information and product history table in the products table how do I do this in a single query?

my sample code:

    <table> 
    <thead> 
        <tr> 
            <th>Product</th>
            <th>Model</th>
            <th>Actıve Total</th>
            <th>Pasıve Total</th>
            <th>Color Total</th>
        </tr> 
    </thead> 
    <tbody> 
<?php 

$product = $db->get_results("SELECT * FROM product WHERE (status='ACTIVE')");


foreach ($product as $p ){

$active_total   = $db->get_var("SELECT SUM(price) FROM product_history WHERE pid='$p->pid' AND status='AKTIVE'");
$pasive_total   = $db->get_var("SELECT SUM(price) FROM product_history WHERE pid='$p->pid' AND status='PASIVE'");
$color_total    = $db->get_var("SELECT SUM(price) FROM product_history WHERE pid='$p->pid' AND status='ACTIVE' AND color='BLUE'");
?>      
        <tr> 
            <td><?php echo $p->name; ?></td>
            <td><?php echo $p->model; ?></td> 
            <td><?php echo $active_total; ?></td> 
            <td><?php echo $pasive_total; ?></td> 
            <td><?php echo $color_total; ?></td>
         </tr> 
<?php } ?>
    </tbody> 
</table>

enter image description here

enter image description here

Thanks.

  • 写回答

2条回答 默认 最新

  • duanqiang6501 2018-11-01 22:55
    关注

    You can use conditional aggregation to get these results from one query:

    $sql = "SELECT
                SUM(CASE WHEN status='AKTIVE' THEN price ELSE 0 END) AS aktive,
                SUM(CASE WHEN status='PASIVE' THEN price ELSE 0 END) AS pasive,
                SUM(CASE WHEN status='AKTIVE' AND color='BLUE' THEN price ELSE 0 END) AS color,
            FROM product_history 
            WHERE pid='$p->pid'");
    

    You could actually merge both your queries into 1 using a LEFT JOIN:

    $sql = "SELECT p.*,
                   SUM(CASE WHEN h.status='AKTIVE' THEN h.price ELSE 0 END) AS aktive,
                   SUM(CASE WHEN h.status='PASIVE' THEN h.price ELSE 0 END) AS pasive,
                   SUM(CASE WHEN h.status='AKTIVE' AND h.color='BLUE' THEN h.price ELSE 0) AS color
            FROM product p
            LEFT JOIN product_history h ON h.pid = p.pid
            WHERE p.status='ACTIVE'
            GROUP BY p.pid";
    

    If the only value of interest from product is pid (for the purposes of matching against the pid value in product_history) you should change the p.* in the query to p.pid. Otherwise you should ideally enumerate the specific fields whose values you want. See Why is SELECT * considered harmful?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改