如何在 yii2 的左连接中分配我需要数据的方法?
How to assign in a method that I need data in a left join in yii2?
如何在 yii2 的左连接中分配我需要数据的方法?
我有一个 sql 查询:
$query
->leftJoin('oper_coming', 'oper_coming.product_id = ' . $product_id)
->leftJoin('oper_adjustment', 'oper_adjustment.product_id = ' . $product_id)
->leftJoin('oper_consumption', 'oper_consumption.product_id = ' . $product_id)
->andWhere(['in', 'operations.id', ['oper_coming.transaction_id', 'oper_adjustment.transaction_id', 'oper_consumption.transaction_id']])
->groupBy('operations.id');
如何从左连接赋值?
Yii2 被认为是一个字符串。
->andWhere(['in', 'operations.id', ['oper_coming.transaction_id', 'oper_adjustment.transaction_id', 'oper_consumption.transaction_id']])
而我需要从左联接中替换值
首先,始终(或几乎总是)为您的表使用别名,以保持整洁和方便您的手指。
->leftJoin('oper_coming oc', , 'oc.product_id = ' . $product_id)
现在您可以使用 oc.transaction_id
.
而不是 oper_coming.transaction_id
其次,SQL 不是那样工作的,如果你想将所有 ID 都放在你的 andWhere 中,你必须执行一个子 SELECT
以首先获得所有所需的 ID。
但是如果没有带有 INNER JOIN
.
的子选择,你会做不同的事情
将所有 leftJoin
更改为 innerJoin
,然后添加检查以确保表的 ID 不为空,例如:
->andWhere(['IS NOT', 'oper_coming.transaction_id', null])
->andWhere(['IS NOT', 'oper_adjustment.transaction_id', null])
->andWhere(['IS NOT', 'oper_consumption.transaction_id', null])
如何在 yii2 的左连接中分配我需要数据的方法?
我有一个 sql 查询:
$query
->leftJoin('oper_coming', 'oper_coming.product_id = ' . $product_id)
->leftJoin('oper_adjustment', 'oper_adjustment.product_id = ' . $product_id)
->leftJoin('oper_consumption', 'oper_consumption.product_id = ' . $product_id)
->andWhere(['in', 'operations.id', ['oper_coming.transaction_id', 'oper_adjustment.transaction_id', 'oper_consumption.transaction_id']])
->groupBy('operations.id');
如何从左连接赋值? Yii2 被认为是一个字符串。
->andWhere(['in', 'operations.id', ['oper_coming.transaction_id', 'oper_adjustment.transaction_id', 'oper_consumption.transaction_id']])
而我需要从左联接中替换值
首先,始终(或几乎总是)为您的表使用别名,以保持整洁和方便您的手指。
->leftJoin('oper_coming oc', , 'oc.product_id = ' . $product_id)
现在您可以使用 oc.transaction_id
.
oper_coming.transaction_id
其次,SQL 不是那样工作的,如果你想将所有 ID 都放在你的 andWhere 中,你必须执行一个子 SELECT
以首先获得所有所需的 ID。
但是如果没有带有 INNER JOIN
.
将所有 leftJoin
更改为 innerJoin
,然后添加检查以确保表的 ID 不为空,例如:
->andWhere(['IS NOT', 'oper_coming.transaction_id', null])
->andWhere(['IS NOT', 'oper_adjustment.transaction_id', null])
->andWhere(['IS NOT', 'oper_consumption.transaction_id', null])