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语句,省去很多代码。