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