SQL Yii2innerJoin()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32480792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Yii2 innerJoin()
提问by sdafasdf
I want to implement a sql query in the following way:
我想通过以下方式实现一个sql查询:
INNER JOIN
`Product_has_ProductFeature` t ON `Product`.`id` = t.`productId` AND t.`productFeatureValueId` = 1
INNER JOIN
`Product_has_ProductFeature` t1 ON `Product`.`id` = t1.`productId` AND t1.`productFeatureValueId` = 5
How can I do this using innerJoin()
or something like above mentioned?
我怎样才能使用innerJoin()
上面提到的或类似的东西来做到这一点?
采纳答案by Sageth
innerJoin()
is a methodfrom the Queryclass.
You can try something like this.
你可以尝试这样的事情。
$query = new \yii\db\Query;
$command = $query->innerJoin(
'Product_has_ProductFeature',
`Product`.`id` = t.`productId`)
->andWhere('t.`productFeatureValueId` = 1')
->createCommand();
$queryResult = $command->query();
回答by Amin Shafiee
You can use the below code:
您可以使用以下代码:
$model = Product::find()
->innerJoinWith('t', 'Product.id = T.productId')
->andWhere(['T.productFeatureValueId' => ''])
->innerJoinWith('t1', 'Product.id = T1.productId')
->andWhere(['T1.productFeatureValueId' => '5'])
->all();
回答by mohammad nazari
$query = new \yii\db\Query();
$query->from(['u' => 'usr_user'])
->select(['u.id','p.first_name','p.last_name'])
->innerJoin(['i' => 'pdl_inspector'],'`u`.`id` = `inspector_id`')
->innerJoin(['p'=>'usr_profile'],'`p`.`user_id` = `u`.`id`')
->all();
回答by Davron Achilov
Tables vote_results, vote_answers, vote_questions
Relations
1. vote_results > vote_answers (answer_id)
2. vote_answers > vote_questions (question_id)
表vote_results、vote_answers、vote_questions
关系
1.vote_results>vote_answers(answer_id)
2.vote_answers>vote_questions(question_id)
$matches = Results::find()
->select(['vote_results.id', 'COUNT(vote_results.answer_id) as MatchCount'])
->innerJoin('vote_answers', 'vote_results.answer_id = vote_answers.id')
->innerJoin('vote_questions', 'vote_answers.question_id = vote_questions.id')
->andWhere(['in', 'vote_results.answer_id', 31])
->having('COUNT(vote_results.id)>=1')
->orderBy('MatchCount DESC')
->asArray()
->one();
var_dump($matches['MatchCount']);