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条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题