Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

可以讲一讲为什么后台的首页sql全都是错了的么 #46

Open
honece opened this issue May 21, 2023 · 0 comments
Open

可以讲一讲为什么后台的首页sql全都是错了的么 #46

honece opened this issue May 21, 2023 · 0 comments

Comments

@honece
Copy link

honece commented May 21, 2023

三条sql

        // 会员订单总数和订单总购物额
        // $order = OrderModel::field("COUNT(*) AS order_num,order_amount")->where("status",5)->find();
        $order = OrderModel::fieldRaw("COUNT(*) AS order_num,SUM(order_amount) AS order_amount")->where("status", 5)->find();
        // $usersGoodsCount = OrderModel::withJoin("users")->field("COUNT(DISTINCT order.user_id) as count")->where("order.status",5)->find();
        $usersGoodsCount = OrderModel::field('user_id')->distinct("user_id")->where("status", 5)->count();
        // 订单排行
        // $orderHot = OrderModel::withJoin("users")->field("SUM(order.order_amount) as total,COUNT(order.id) as count,users.username")->where("order.status",5)->group("order.user_id")->order("total","DESC")->limit(10)->select()->toArray();
        $orderHot = OrderModel::
            hasWhere(
                'users',
                ["Order.status" => 5],
                'Users.username,SUM(Order.order_amount) as total,COUNT(Order.id) as count'
            )
            ->group('Order.user_id')->order("total", "DESC")->limit(10)->select()->toArray();

        foreach ($orderHot as $k => $v) {
            $orderHot[$k]['p'] = $k + 1;
        }

        // $data = OrderModel::field('SUM(order_amount) as amount,count(*) as total,create_time,FROM_UNIXTIME(create_time,"%Y%m%d") as days')
        //     ->where("status", "in", "2,5")
        //     ->group('days having create_time >= ' . strtotime(date('Y-m-d 00:00:00', strtotime("-30 day"))) . ' and create_time <= ' . strtotime(date('Y-m-d 23:59:59', time())))->select()->toArray();

        $data = OrderModel::fieldRaw('SUM(order_amount) as amount,count(*) as total,FROM_UNIXTIME(create_time,"%Y%m%d") as days')
            ->where("status", "in", "2,5")
            ->where('create_time >= ' . strtotime(date('Y-m-d 00:00:00', strtotime("-30 day"))))
            ->where('create_time <= ' . strtotime(date('Y-m-d 23:59:59', time())))
            ->group('days')->select()->toArray();

一条return

        // "f"                      => empty($order["order_amount"]) ? "0.00" : number_format(($order["order_amount"] > 0 && $usersGoodsCount["count"] > 0 ? $order["order_amount"] / $usersGoodsCount["count"] : 0), 2),
        "f"                      => empty($order["order_amount"]) ? "0.00" : number_format(($order["order_amount"] > 0 && $usersGoodsCount > 0 ? $order["order_amount"] / $usersGoodsCount : 0), 2),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant