原始查询转换为Eloquent(Raw query convert to Eloquent)

编程入门 行业动态 更新时间:2024-10-24 10:26:36
原始查询转换为Eloquent(Raw query convert to Eloquent)

需要帮助将此查询转换为雄辩,以便使用paginate,groupby和orderby方法

SELECT DATE(created_at), SUM(CASE WHEN `type` = 'withdraw' THEN 1 ELSE 0 END) AS 'withdraw_count', SUM(CASE WHEN `type` = 'withdraw' THEN amount ELSE 0 END) AS 'withdraw_amount', SUM(CASE WHEN `type` = 'deposit' THEN 1 ELSE 0 END) AS 'deposit_count', SUM(CASE WHEN `type` = 'deposit' THEN amount ELSE 0 END) AS 'deposit_amount' FROM ( SELECT created_at, 'withdraw' as `type`, amount FROM withdraw UNION ALL SELECT created_at, 'deposit' as `type`, amount FROM deposit ) t GROUP by DATE(created_at)

Need help in converting this query to eloquent for me to use the paginate, groupby and orderby method

SELECT DATE(created_at), SUM(CASE WHEN `type` = 'withdraw' THEN 1 ELSE 0 END) AS 'withdraw_count', SUM(CASE WHEN `type` = 'withdraw' THEN amount ELSE 0 END) AS 'withdraw_amount', SUM(CASE WHEN `type` = 'deposit' THEN 1 ELSE 0 END) AS 'deposit_count', SUM(CASE WHEN `type` = 'deposit' THEN amount ELSE 0 END) AS 'deposit_amount' FROM ( SELECT created_at, 'withdraw' as `type`, amount FROM withdraw UNION ALL SELECT created_at, 'deposit' as `type`, amount FROM deposit ) t GROUP by DATE(created_at)

最满意答案

$from = DB::table('withdraw')
    ->select('created_at', DB::raw("'withdraw' as `type`"), 'amount')
    ->unionAll(
        DB::table('deposit')
            ->select('created_at', DB::raw("'deposit' as `type`"), 'amount')
    );
DB::query()
    ->selectRaw('DATE(created_at)')
    ->selectRaw("SUM(CASE WHEN `type` = 'withdraw' THEN 1 ELSE 0 END) AS 'withdraw_count'")
    ->selectRaw("SUM(CASE WHEN `type` = 'withdraw' THEN amount ELSE 0 END) AS 'withdraw_amount'")
    ->selectRaw("SUM(CASE WHEN `type` = 'deposit' THEN 1 ELSE 0 END) AS 'deposit_count'")
    ->selectRaw("SUM(CASE WHEN `type` = 'deposit' THEN amount ELSE 0 END) AS 'deposit_amount'")
    ->fromSub($from, 't')
    ->groupBy(DB::raw('DATE(created_at)'))
    ->get();
$from = DB::table('withdraw')
    ->select('created_at', DB::raw("'withdraw' as `type`"), 'amount')
    ->unionAll(
        DB::table('deposit')
            ->select('created_at', DB::raw("'deposit' as `type`"), 'amount')
    );
DB::query()
    ->selectRaw('DATE(created_at)')
    ->selectRaw("SUM(CASE WHEN `type` = 'withdraw' THEN 1 ELSE 0 END) AS 'withdraw_count'")
    ->selectRaw("SUM(CASE WHEN `type` = 'withdraw' THEN amount ELSE 0 END) AS 'withdraw_amount'")
    ->selectRaw("SUM(CASE WHEN `type` = 'deposit' THEN 1 ELSE 0 END) AS 'deposit_count'")
    ->selectRaw("SUM(CASE WHEN `type` = 'deposit' THEN amount ELSE 0 END) AS 'deposit_amount'")
    ->fromSub($from, 't')
    ->groupBy(DB::raw('DATE(created_at)'))
    ->get();

                    
                     
          

更多推荐

本文发布于:2023-07-04 17:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1027036.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   原始   Eloquent   convert   query

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!