假设我有3个不同的表,它们具有相同的外键,如下所示:-
表一产品单位
| id | product_id | weight | status_id |
|:----|--------------|---------|------------:|
| 1 | 4 | 300 | 1 |
| 2 | 5 | 120 | 2 |
表二产品包装
| id | product_id | weight | status_id |
|:----|--------------|---------|------------:|
| 1 | 4 | 1.2 | 1 |
| 2 | 5 | 480 | 1 |
表三产品纸箱
| id | product_id | weight | status_id |
|:----|--------------|---------|------------:|
| 1 | 4 | 10.2 | 1 |
| 2 | 5 | 4.8 | 2 |
其中表产品和状态表如下所示:-
a。 [产品表格
| id | name |
|:----|--------------:|
| 4 | Choco Cake |
| 5 | Hazelnut Bun |
b。 状态表格
| id | description |
|:----|--------------:|
| 1 | Available |
| 2 | Unavailable |
如何通过1查询从这3个表(product_unit,product_package和product_carton)中获取所有状态?
到目前为止,我已经可以做到这一点:-
$product_info = Product::find()
->select([
'product.name AS productName',
'product_unit.weight AS weightUnit',
'product_package.weight AS weightPackage',
'product_carton.weight AS weightCarton',
'status.description AS statusDesc'])
->leftJoin('product_unit', 'product.id = product_unit.product_id')
->leftJoin('product_package', 'product.id = product_package.product_id')
->leftJoin('product_carton', 'product.id = product_carton.product_id')
->leftJoin('status', 'status.id = product_unit.status_id')
->asArray()
->all();
使用上面的查询我可以从仅产品单元表]中获得'statusDesc',因为我'具有产品单元表的'leftJoin()'状态表。
我如何JOIN
其他2个表(product_package和product_carton),并从中也获得statusDesc?有什么办法可以通过1 Query来做到这一点?假设我有3个不同的表,它们具有相同的外键,如下所示:-表1 product_unit | id | product_id |重量status_id | |:---- | -------------- || -------- | ------------:| | ...
$product_info = Product::find()
->select([
'product.name AS productName',
'product_unit.weight AS weightUnit',
'product_package.weight AS weightPackage',
'product_carton.weight AS weightCarton',
's1.description AS status_unit_Desc'])
's2.description AS status_package_Desc'])
's3.description AS status_carton_Desc'])
->leftJoin('product_unit', 'product.id = product_unit.product_id')
->leftJoin('product_package', 'product.id = product_package.product_id')
->leftJoin('product_carton', 'product.id = product_carton.product_id')
->leftJoin('status AS s1', 's1.id = product_unit.status_id')
->leftJoin('status AS s2', 's2.id = product_package.status_id')
->leftJoin('status AS s3', 's3.id = product_carton.status_id')
->asArray()
->all();