Yii2 innerJoin()

sda*_*sdf 5 sql inner-join yii2

我想以下列方式实现一个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
Run Code Online (Sandbox Code Playgroud)

我怎么能用innerJoin()上面提到的东西呢?

Ami*_*iee 13

您可以使用以下代码:

$model = Product::find()
->innerJoinWith('t', 'Product.id = T.productId')
->andWhere(['T.productFeatureValueId' => ''])
->innerJoinWith('t1', 'Product.id = T1.productId')
->andWhere(['T1.productFeatureValueId' => '5'])
->all();
Run Code Online (Sandbox Code Playgroud)


Sag*_*eth 5

innerJoin()是一个方法查询类。

您可以尝试这样。

$query = new \yii\db\Query;
$command = $query->innerJoin(
         'Product_has_ProductFeature',
         `Product`.`id` = t.`productId`)
     ->andWhere('t.`productFeatureValueId` = 1')
     ->createCommand();
$queryResult = $command->query();
Run Code Online (Sandbox Code Playgroud)