如何合并查询yii2

r6hnlfcb  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(208)

我有一个包含两个表的查询。我想将这两个查询合并为一个查询。如何操作?

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
打印($query); result

zzlelutf

zzlelutf1#

您提供的原始查询没有主查询的任何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,
]);
gmol1639

gmol16392#

为了合并来自2个不同表的2个查询的结果,您可以使用Union Operator。不过,从链接中可以看到一些必要的条件:

  1. UNION中的每个SELECT语句必须具有相同的列数
    1.这些列还必须具有相似的数据类型
    1.每个SELECT语句中的列也必须具有相同的顺序
    为了使此功能为您所用,您需要按如下方式更改查询:
$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');

简而言之,如果第一个表包含列a和列b,第二个表包含列a和列c。那么您需要从第一个表中获取select(['a','b','NULL as c']),从第二个表中获取select(['a','NULL as b','c']),以便将两个结果合并
那么组合查询就是:

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

相关问题