使用表达式Yii2选择字符串被检测为列
我有这样一个问题:使用表达式Yii2选择字符串被检测为列,yii2,expression,Yii2,Expression,我有这样一个问题: use yii\db\Expression; public function search($params) { $query = (new \yii\db\Query()) ->select([ "ir.id", "ir.no_surat", "tc.level", "nominal" => 'tc.cleaning'
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");
}
$cleanigQuery->union($oneBarQuery, true);
$query = (new \yii\db\Query())
->select('*')
->from(['u' => $cleanigQuery])
->orderBy('no_surat');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
请参见新表达式(“像塔吉汉一样干净”),
Yii2检测到clean
为列。但我需要tagihan列上的clean as值
+----+-------------+
| id | tagihan |
+----+-------------+
| 1 | clean |
| 2 | clean |
+----+-------------+
更新
义乌联盟2
$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");
所以,我可以这样做:
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");
}
$cleanigQuery->union($oneBarQuery, true);
$query = (new \yii\db\Query())
->select('*')
->from(['u' => $cleanigQuery])
->orderBy('no_surat');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
在这种情况下,您可以使用文字select语句
->select("ir.id, ir.no_surat, tc.level,
tc.cleaning, 'clean' as tagihan, tc.ditagihkan_bulan")
在这种情况下,您可以使用文字select语句
->select("ir.id, ir.no_surat, tc.level,
tc.cleaning, 'clean' as tagihan, tc.ditagihkan_bulan")
像这样:
$query->select(“*,CONCAT('clean')as tagihan”)
像这样:$query->select(“*,CONCAT('clean')as tagihan”)
我已经尝试过很多次了,但仍然是“field list”中未知的列“clean”,请确保您有一个干净的列,如果可能的话,您必须分配表别名。。回答更新我很确定,我没有一个列的名称为“clean”,如果你没有一个列的名称为clean。。你为什么要选择它。。或者更好,你说的“像tagihan一样干净”是什么意思?在这种情况下,你需要一个文本值,例如:“干净”像tagihan。。答案已更新。我已经用这种方法尝试了很多时间,但仍然是“字段列表”中的未知列“clean”。请确保您有一个clean列,如果可以,则必须分配表别名。。回答更新我很确定,我没有一个列的名称为“clean”,如果你没有一个列的名称为clean。。你为什么要选择它。。或者更好,你说的“像tagihan一样干净”是什么意思?在这种情况下,你需要一个文本值,例如:“干净”像tagihan。。答案已更新。