Php 有说服力的如何构建查询以从中求和子查询的总数?

Php 有说服力的如何构建查询以从中求和子查询的总数?,php,database,eloquent,subquery,union,Php,Database,Eloquent,Subquery,Union,我已经构建了MySql查询语句,其中包含我需要的结果,下面是一个接近实际情况的小示例: MYSQL SELECT Total, SUM(Hours) SUM(Charge) AS Charge, SUM(Paid) AS Paid FROM ( SELECT Total AS Total, SUM(Hours) AS Hours,

我已经构建了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;
这里是到目前为止创建的ORM查询,我在其中完成了从union获取结果的任务,但仍然缺少在from中插入“$all”查询

做了2个子查询,在第3个查询中添加了union,所以它可以正常工作,但仍然无法确定如何创建初始select语句来合并内部的所有子查询

能言善辩

$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();

任何帮助都可以解决这个问题。

阅读并更好地理解雄辩的查询生成器帮助类后,找出我问题的答案

由于查询生成器在后台使用PDO,因此我们知道有一种方法可以将参数绑定到查询,从而清理绑定的变量

现在,正如您所看到的,任意(原始)查询是在查询生成器中使用DB::select()方法完成的。让我们看看Lightlight\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());
    });
}
对我的初始查询做了一些调整,DB:select将从子查询$all->getBindings()中获取的绑定作为第二个参数,然后将其解析

解决方案:

$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();
希望这能帮助有同样问题的人。谢谢!:)