zoukankan      html  css  js  c++  java
  • case when进行数据统计

    SELECT 
    SUM(CASE WHEN `status` = '1'  THEN 1 ELSE 0 END) AS waitpay,
    SUM(CASE WHEN `group_done` = '1' AND `status` = 0 THEN 1 ELSE 0 END) AS waitgroup,
    SUM(CASE WHEN `status` = '3' THEN 1 ELSE 0 END) AS waitreceive,
    SUM(CASE WHEN `status` = '4' AND `comment` = 1 THEN 1 ELSE 0 END) AS waitcomment,
    SUM(CASE WHEN `back_status` <> '0' THEN 1 ELSE 0 END) AS aftersell
     FROM `order`  WHERE uid = 145 AND `is_del` = 0 AND `close_reason` = 0`user`
    
    
    /**
         * 统计订单的数量
         */
        public function getStatOrderNum() {
            $needParam = array(
                'openId'=>array('code'=>1,'msg'=>'缺少参数')
            );
            $this->regArguments($needParam,$this->param);
            if($this->outData['code']){
                $this->printOut();
            }
            $uModel = new UserModel();
            $uid = $uModel->getUidByOpenId($this->param['openId']);
            if(!$uid){
                $this->outData['code'] = 1;
                $this->outData['msg']    = '账号异常,请联系客服人员';
                $this->printOut();
            }
    
            // 获取待付款,待成团,待收货,待评价,售后订单的数量
    
            $sql = "SELECT";
            $sql .=" SUM(CASE WHEN `status` = '1'  THEN 1 ELSE 0 END) AS waitpay,";
            $sql .=" SUM(CASE WHEN `group_done` = '1' AND `status` = 0 THEN 1 ELSE 0 END) AS waitgroup,";
            $sql .=" SUM(CASE WHEN `status` = '3' THEN 1 ELSE 0 END) AS waitreceive,";
            $sql .=" SUM(CASE WHEN `status` = '4' AND `comment` = 1 THEN 1 ELSE 0 END) AS waitcomment,";
            $sql .=" SUM(CASE WHEN `back_status` <> '0' THEN 1 ELSE 0 END) AS aftersell";
            $sql .=" FROM `order`  WHERE uid = ".$uid." AND `is_del` = 0 AND `close_reason` = 0";
    
    
            $orderModel = new OrderModel();
            $res = $orderModel->query($sql);
            $this->outData['data'] = $res[0];
            $this->printOut();
        }
    

    一条sql语句,省去很多代码。

  • 相关阅读:
    excel导入phpmyadmin
    Python 教程资源
    SAE部署Python-让云端自动运行Python代码
    python socket 超时设置 errno10054
    使用python一步一步搭建微信公众平台(一)
    PHP读取MySQL数据
    php连接mysql并读取数据
    php分页例子实现读取mysql数据分页显示
    51Nod1305 Pairwise Sum and Divide
    SPOJ SUBXOR
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/8583058.html
Copyright © 2011-2022 走看看