Laravel / Eloquent中的子查询无法正常工作

时间:2018-11-26 06:20:45

标签: php laravel eloquent

我目前正在基于Lumen的应用程序上工作,并且无法使用Eloquent / Laravel查询。我目前正在从事的工作是:

$min = $filter['min_spending']*100;

$query = User::isCustomer()
    ->with("interests")
    ->leftJoin("points_history", "points_history.user_id", "=", "users.id")
    ->leftJoin("collections", "collections.user_id", "=", "users.id")
    ->select("users.*",
            DB::raw("(SELECT SUM(amount) FROM points_history
                      WHERE points_history.user_id = users.id
                      AND points_history.confirmed = true ) as total_points"),
            DB::raw("(SELECT SUM(price) FROM collections
                      WHERE paid_at IS NOT NULL 
                      AND refunded_at IS NULL
                      AND collections.user_id = users.id) as total_purchase"),
            DB::raw("(SELECT SUM(collections.price)) as current_purchase"))
            ->where("total_purchase", ">=", $min);

// other relevant code here

$query->groupBy('users.id')->orderBy('users.created_at', 'desc');

但是,当我尝试获得结果时,出现此错误:

{"error":"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'total_purchase >=' in 'where clause' (SQL: select count(*) as aggregate from `users` left join `points_history` on `points_history`.`user_id` = `users`.`id` left join `collections` on `collections`.`user_id` = `users`.`id` where `users`.`deleted_at` is null and `role` = 2 and `total_purchase >=` = 200000 group by `users`.`id`)"}

子查询似乎没有注册,这导致了问题(?)我想要的SQL语句是:

SELECT users . *, 
                    (SELECT 
                        SUM(amount)
                    FROM
                        points_history
                    WHERE
                        points_history.user_id = users.id
                            AND points_history.confirmed = true)  AS total_points, (SELECT 
                        SUM(price)
                    FROM
                        collections
                    WHERE
                        paid_at IS NOT NULL
                            AND refunded_at IS NULL
                            AND collections.user_id = users.id) AS total_purchase,  SUM(collections.price) AS current_purchase
                    from
                        users
                            left join
                        points_history ON points_history.user_id = users.id
                            left join
                        collections ON collections.user_id = users.id
                    where
                        users.deleted_at is null AND (SELECT 
                                SUM(price)
                            FROM
                                collections
                            WHERE
                                paid_at IS NOT NULL
                                    AND refunded_at IS NULL
                                    AND collections.user_id = users.id) >= 200000
GROUP BY users.id

其中200000是$ min。谁能帮助我查明查询中的问题,以便找出解决方法?

3 个答案:

答案 0 :(得分:0)

    ->where("total_purchase",">=", $min);

在快速查看中,您没有在最后一行添加“,”

答案 1 :(得分:0)

您应该尝试以下操作:

$min = $filter['min_spending']*100;

$query = User::isCustomer()
    ->with("interests")
    ->leftJoin("points_history", "points_history.user_id", "=", "users.id")
    ->leftJoin("collections", "collections.user_id", "=", "users.id")
    ->select("users.*",
            DB::raw("(SELECT SUM(amount) FROM points_history
                      WHERE points_history.user_id = users.id
                      AND points_history.confirmed = true ) as total_points"),
            DB::raw("(SELECT SUM(price) FROM collections
                      WHERE paid_at IS NOT NULL 
                      AND refunded_at IS NULL
                      AND collections.user_id = users.id) as total_purchase"),
            DB::raw("(SELECT SUM(collections.price)) as current_purchase"))
            ->where("total_purchase", '>=', $min);

// other relevant code here

$query->groupBy('users.id')->orderBy('users.created_at', 'desc');

答案 2 :(得分:0)

已修复:

很显然,SELECT不需要多个参数,但是可以接受一个数组

->select(["users.*",
                    DB::raw("(SELECT SUM(amount) FROM points_history
                                WHERE points_history.user_id = users.id
                                AND points_history.confirmed = true ) as total_points"),
                    DB::raw("(SELECT SUM(price) FROM collections
                                WHERE paid_at IS NOT NULL 
                                AND refunded_at IS NULL
                                AND collections.user_id = users.id) as total_purchase"),
                    DB::raw("(SELECT SUM(collections.price)) as current_purchase")]

最终也将WHERE子句更改为

$query->whereRaw(DB::raw("(SELECT SUM(amount) FROM points_history
       WHERE points_history.user_id = users.id
       AND points_history.confirmed = true ) <= " . $max));

以点而不是逗号连接到查询