Yii2 innerJoin()
Yii2 innerJoin()
我想通过以下方式实现 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
我如何使用 innerJoin()
或上述类似的方法来做到这一点?
innerJoin()
是 method from the Query class.
你可以尝试这样的事情。
$query = new \yii\db\Query;
$command = $query->innerJoin(
'Product_has_ProductFeature',
`Product`.`id` = t.`productId`)
->andWhere('t.`productFeatureValueId` = 1')
->createCommand();
$queryResult = $command->query();
您可以使用以下代码:
$model = Product::find()
->innerJoinWith('t', 'Product.id = T.productId')
->andWhere(['T.productFeatureValueId' => ''])
->innerJoinWith('t1', 'Product.id = T1.productId')
->andWhere(['T1.productFeatureValueId' => '5'])
->all();
表 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']);
$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();
我想通过以下方式实现 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
我如何使用 innerJoin()
或上述类似的方法来做到这一点?
innerJoin()
是 method from the Query class.
你可以尝试这样的事情。
$query = new \yii\db\Query;
$command = $query->innerJoin(
'Product_has_ProductFeature',
`Product`.`id` = t.`productId`)
->andWhere('t.`productFeatureValueId` = 1')
->createCommand();
$queryResult = $command->query();
您可以使用以下代码:
$model = Product::find()
->innerJoinWith('t', 'Product.id = T.productId')
->andWhere(['T.productFeatureValueId' => ''])
->innerJoinWith('t1', 'Product.id = T1.productId')
->andWhere(['T1.productFeatureValueId' => '5'])
->all();
表 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']);
$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();