如何将子查询作为表名传递给yii2中的另一个查询 [英] How to pass subquery as table name to another query in yii2
本文介绍了如何将子查询作为表名传递给yii2中的另一个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个查询,试图将其转换为yii2语法.下面是查询
I have a query which I am trying to convert into yii2 syntax. Below is the query
SELECT project_id, user_ref_id FROM
(
SELECT `project_id`, `user_ref_id`
FROM `projectsList`
WHERE user_type_ref_id = 1) AS a WHERE user_ref_id = '.yii::$app->user->id;
我正在尝试将其转换为yii2格式,例如
I am trying to convert it into yii2 format like
$subQuery = (new Query())->select(['p.project_id', 'p.user_ref_id'])->from('projectsList')->where(['user_type_ref_id' => 1]);
$uQuery = (new Query())->select(['p.project_id', 'p.user_ref_id'])->from($subQuery)->where(['user_ref_id ' => yii::$app->user->id])->all();
出现类似
trim() expects parameter 1 to be string, object given
如何将子查询作为表名传递给另一个查询
How to I pass subquery as table name to another query
推荐答案
未经测试,但通常是这样.您需要将subQuery作为表传递.因此,将第二个查询中的->from($subQuery)
更改为->from(['subQuery' => $subQuery])
Not tested, but generally this is how it goes. You need to pass the subQuery as a table. So change ->from($subQuery)
in the second query to ->from(['subQuery' => $subQuery])
$subQuery = (new Query())->select(['p.project_id', 'p.user_ref_id'])->from('projectsList')->where(['user_type_ref_id' => 1]);
然后
$query = (new Query())->select(['p.project_id', 'p.user_ref_id'])->from(['subQuery' => $subQuery])->where(['subQuery.user_ref_id ' => yii::$app->user->id])->all();
这篇关于如何将子查询作为表名传递给yii2中的另一个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文