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

时间:2017-06-26 16:17:01

标签: yii2 expression

我有这样的查询:

<userhome>/
  .IntelliJIdea<version>/
    config/
      settingsRepository/
        repository/
          .gitignore

请参阅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"); } Yii2检测到new Expression("clean as tagihan"),为列。但我需要在tagihan的专栏

上使用clean作为值
clean

更新

YII2中的UNION

+----+-------------+
| 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");

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

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