代码之家  ›  专栏  ›  技术社区  ›  Fadly Dzil

使用表达式Yii2选择字符串检测为列

  •  1
  • Fadly Dzil  · 技术社区  · 7 年前

    我有这样一个问题:

    use yii\db\Expression;
    
    public function search($params)
    {
    $query = (new \yii\db\Query())
            ->select([
                    "ir.id",
                    "ir.no_surat",
                    "tc.level",
                    "nominal" => 'tc.cleaning',
                    new Expression("clean as tagihan"),
                    "tc.ditagihkan_bulan"
                ])
    
            ->from('ydepotras_estimator.repair_estimate as re')
            ->leftJoin(['ydepotras_estimator.inspection_report as ir'],
                "ir.id = re.inspection_id")
            ->innerJoin(['ydepotras_finance.tagihan_cleaning as tc'],
                " re.id = tc.repair_estimate_id");
    }
    

    new Expression("clean as tagihan"), 检测到Yii2 clean 作为列。但我需要塔吉汉列上的clean as值

    +----+-------------+
    | id |   tagihan   |
    +----+-------------+
    | 1  |   clean     |
    | 2  |   clean     |
    +----+-------------+
    

    使现代化

    $cleanigQuery = (new \yii\db\Query())
            ->select([
                "ir.id",
                "ir.no_surat",
                "tc.level",
                "nominal" => 'tc.cleaning',
                new Expression("clean as tagihan"),
                "tc.ditagihkan_bulan"
            ])
            ->from('ydepotras_estimator.repair_estimate as re')
            ->leftJoin(['ydepotras_estimator.inspection_report as ir'],
                "ir.id = re.inspection_id")
            ->innerJoin(['ydepotras_finance.tagihan_cleaning as tc'],
                " re.id = tc.repair_estimate_id");
    
    $oneBarQuery = (new \yii\db\Query())
            ->select([
                "ir.id",
                "ir.no_surat",
                "tob.level",
                "nominal" => 'tob.one_bar',
                    new Expression("one_bar as tagihan"),
                "tob.ditagihkan_bulan"
            ])
            ->from('ydepotras_estimator.repair_estimate as re')
            ->leftJoin(['ydepotras_estimator.inspection_report as ir'],
                "ir.id = re.inspection_id")
            ->innerJoin(['ydepotras_finance.tagihan_one_bar as tob'],
                " re.id = tob.repair_estimate_id");
    

    所以,我可以这样做:

    $cleanigQuery->union($oneBarQuery, true);
    
        $query = (new \yii\db\Query())
            ->select('*')
            ->from(['u' => $cleanigQuery])
            ->orderBy('no_surat');
    
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
    
        ]);
    
    2 回复  |  直到 7 年前
        1
  •  4
  •   dataskills    6 年前

    这样地: $query->select("*,CONCAT('clean') as tagihan")

        2
  •  1
  •   ScaisEdge    7 年前

    在这种情况下,可以使用文字选择语句

              ->select("ir.id, ir.no_surat, tc.level, 
                    tc.cleaning, 'clean' as tagihan, tc.ditagihkan_bulan")