简体   繁体   English

雄辩的如何建立查询以求和子查询的总数?

[英]Eloquent how to build query to sum totals subqueries inside from?

I have already built the MySql query statements in which have the results i needed, below the small example closed to real situation: 我已经建立了MySql查询语句,其中包含我需要的结果,下面这个小例子适合实际情况:

MYSQL MySQL数据库

SELECT  
        Total,
        SUM(Hours)
        SUM(Charge) AS Charge,
        SUM(Paid) AS Paid
FROM    (   SELECT  
                    Total AS Total,
                    SUM(Hours) AS Hours,
                    SUM(Charge) AS Charge,
                    NULL AS Paid
            FROM    Table
            WHERE   type = 'regular'
            GROUP BY id
            UNION
            SELECT  
                    Total AS Total,
                    SUM(Hours) AS Hours,
                    SUM(Charge) AS Charge,
                    NULL AS Paid
            FROM    Table
            WHERE   type = 'credit'
            GROUP BY id
            UNION
            SELECT  
                    Total AS Total,
                    SUM(Hours) AS Hours,
                    SUM(Charge) AS Charge,
                    NULL AS Paid
            FROM    Table
            WHERE   type = 'tax'
            GROUP BY id
        ) t
GROUP BY Total;

Here is the ORM query so far created in which i accomplish to get the results from the union, but still missing to insert the '$all" query inside the from. 这是到目前为止创建的ORM查询,在该查询中我完成了从联合中获取结果的操作,但是仍然缺少在from中插入“ $ all”查询的方法。

Made 2 subquery, on the 3rd query added the union, so fare it work's, but still can not figure out, how to create the initial select statement to merge all subqueries inside from. 进行了2个子查询,在第3个查询上添加了并集,这样就可以正常工作了,但仍然无法弄清楚,如何创建初始select语句以合并其中的所有子查询。

ELOQUENT 雄辩

$credit = DB::table('table')
->selectRaw('
          Total AS Total,
          SUM(Hours) AS Hours,
          SUM(Charge) AS Charge,
          NULL AS Paid
')
->where('type', '=', 'credit')
->groupBy('id');

$tax = DB::table('table')
->selectRaw('
          Total AS Total,
          SUM(Hours) AS Hours,
          SUM(Charge) AS Charge,
          NULL AS Paid
')
->where('type', '=', 'tax')
->groupBy('id');

$all = DB::table('table')
->selectRaw('
          Total AS Total,
          SUM(Hours) AS Hours,
          SUM(Charge) AS Charge,
          NULL AS Paid
')
->where('type', '=', 'regular')
->groupBy('id')
->union($credit)
->union($tax);

$totals = DB::selectRaw('
              Total,
              SUM(Hours),
              SUM(Charge),
              Paid
          ')
          ->from( $all->toSql() )
          ->groupBy('Total')
          ->get();

Any help will be great to figure this out!. 任何帮助将非常有助于解决这个问题!

After reading and getting a better understanding of the Eloquent query builder helper class, figure the answer to my question. 阅读并更好地理解Eloquent查询构建器帮助器类后,请找出我问题的答案。

Since the query builder is using PDO in the background, we know there is a way to bind parameters to our query so it will sanitize the bound variables. 由于查询生成器在后台使用PDO,因此我们知道有一种将参数绑定到查询的方法,这样可以清理绑定的变量。

Now, as you've seen, arbitrary (raw) queries are done in the query builder using the DB::select() method. 如您所见,现在,您可以使用DB :: select()方法在查询生成器中完成任意(原始)查询。 Let's look at the select() method in Illuminate\\Database\\Connection to see if it has any way to bind our parameters: 让我们看一下Illuminate \\ Database \\ Connection中的select()方法,看它是否可以绑定参数:

public function select($query, $bindings = array())
{
    return $this->run($query, $bindings, function($me, $query, $bindings)
    {
        if ($me->pretending()) return array();

        // For select statements, we'll simply execute the query and return an array
        // of the database result set. Each element in the array will be a single
        // row from the database table, and will either be an array or objects.
        $statement = $me->getPdo()->prepare($query);

        $statement->execute($me->prepareBindings($bindings));

        return $statement->fetchAll($me->getFetchMode());
    });
}

Made some adjustments to my initial query, the DB:select takes as second parameter the bindings which got from sub-query the $all->getBindings() to parse then issued was resolved. 对我的初始查询进行了一些调整,DB:select将第二个参数作为第二个参数,该子查询是从子查询$ all-> getBindings()中解析出来然后进行发布的。

SOLUTION: 解:

$credit = DB::table('table')
->selectRaw('
          Total AS Total,
          SUM(Hours) AS Hours,
          SUM(Charge) AS Charge,
          NULL AS Paid
')
->whereRaw('type = ? ', ['credit'])
->groupBy('id');

$tax = DB::table('table')
->selectRaw('
          Total AS Total,
          SUM(Hours) AS Hours,
          SUM(Charge) AS Charge,
          NULL AS Paid
')
->whereRaw('type = ? ', ['tax'])
->groupBy('id');

$all = DB::table('table')
->selectRaw('
          Total AS Total,
          SUM(Hours) AS Hours,
          SUM(Charge) AS Charge,
          NULL AS Paid
')
->whereRaw('type = ? ', ['regular'])
->groupBy('id')
->union($credit)
->union($tax);

$totals = DB::select("SELECT
              Total,
              SUM(Hours),
              SUM(Charge),
              Paid
              FROM ({$all->toSql()}) AS t
          ", $all->getBindings())
          ->groupBy('Total')
          ->get();

Just hope this help someone with the same problem. 只是希望这可以帮助遇到相同问题的人。 Thanks! 谢谢! :). :)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM