如何组合查询 yii2

how to combine queries yii2

我有一个包含 2 个表的查询。我想将这两个查询合并为一个查询。怎么办?

public function actionGroup()
{
    $query1 = (new \yii\db\Query())
                ->select(['lao',new \yii\db\Expression('COUNT(lao)'),'nama_ptgs', new \yii\db\Expression('SUM(outstanding)')])
                ->from('debitur')
                ->groupBy('lao')
                ->all();

    $query2 = (new \yii\db\Query())
                ->select(['lao', new \yii\db\Expression('SUM(tgt_pergeseran)')])
                ->from('resume')
                ->groupBy('lao')
                ->all();

    return $this->render('outstanding', [
        'query1' => $query1,
        'query2' => $query2,
    ]);
}

示例sql

SELECT debitur.lao, debitur.Outstanding, debitur.jumlah, resume.Target FROM ( SELECT lao, SUM(outstanding) as Outstanding, COUNT(lao) as jumlah FROM debitur GROUP BY lao )debitur INNER JOIN ( SELECT lao, SUM(tgt_pergeseran) as Target FROM resume GROUP BY lao ) resume ON debitur.lao = resume.lao

错误undefined index

打印($查询); result

为了合并来自 2 个不同 table 的 2 个查询的结果,您可以使用 Union Operator。从 link 开始,有必要的条件:

  1. Each SELECT statement within UNION must have the same number of columns
  2. The columns must also have similar data types
  3. The columns in each SELECT statement must also be in the same order

为了使它对您有用,您需要按如下方式更改您的查询:

$query1 = (new \yii\db\Query())
              ->select(['lao',
                         new \yii\db\Expression('COUNT(lao) as count_column'),
                         'nama_ptgs', 
                         new \yii\db\Expression('SUM(outstanding) as sum_column'),
                         new \yii\db\Expression('NULL as tgt_sum')])
              ->from('debitur')
              ->groupBy('lao');

$query2 = (new \yii\db\Query())
                ->select(['lao',
                           new \yii\db\Expression('NULL as count_column'),
                           new \yii\db\Expression('NULL as nama_ptgs'),
                           new \yii\db\Expression('NULL as sum_column'), 
                           new \yii\db\Expression('SUM(tgt_pergeseran) as tgt_sum'),
                ])->from('resume')
                ->groupBy('lao');

简而言之,如果您的第一个 table 包含列 a 和 b,而您的第二个包含列 a 和 c。然后你需要做的是第一个 select(['a','b','NULL as c']) 和第二个 select(['a','NULL as b','c']) 这样你就可以合并两个结果

那么组合查询就是:

$combinedQuery = $query1->union($query2);

您提供的原始查询没有任何where()主查询条件

 SELECT debitur.lao, debitur.Outstanding, debitur.jumlah, resume.Target 
 FROM 
    (SELECT lao, SUM(outstanding) as Outstanding, COUNT(lao) as jumlah FROM debitur GROUP BY lao) debitur 
 INNER JOIN 
    (SELECT lao, SUM(tgt_pergeseran) as Target FROM resume GROUP BY lao) resume 
 ON  debitur.lao = resume.lao

如果这是在 phpmyadmin 窗口中向您显示正确结果的完整且正确的查询,您需要使用如下子查询

$subQueryFrom = new \yii\db\Query();
$subQueryFrom->select(['lao', new \yii\db\Expression('SUM(outstanding) as Outstanding, COUNT(lao) as jumlah')])
    ->from('debitur')
    ->groupBy('lao');

$subQueryJoin = new \yii\db\Query();
$subQueryJoin->select(['lao', new \yii\db\Expression('SUM(tgt_pergeseran) as Target')])
    ->from('resume')
    ->groupBy('lao');

$query = new \yii\db\Query();

$results = $query->select(['debitur.lao', 'debitur.Outstanding', 'debitur.jumlah', 'resume.Target'])
    ->from(['debitur' => $subQueryFrom])
    ->innerJoin(['resume' => $subQueryJoin], 'debitur.lao = resume.lao')
    ->all();

您现在可以在您的视图中使用 $result 来保存针对您的查询的记录。

return $this->render('outstanding', [
        'results' => $results,
]);