简体   繁体   English

3 不同的表具有相同的外键,如何选择

[英]3 Different tables have same Foreign Key, How to select

Supposed I have 3 different tables which have identical Foreign Key like below:-假设我有 3 个不同的表,它们具有相同的外键,如下所示:-

  1. Table One product_unit表一product_unit

     | id | product_id | weight | status_id | |:----|--------------|---------|------------:| | 1 | 4 | 300 | 1 | | 2 | 5 | 120 | 2 |
  2. Table Two product_package表二product_package

     | id | product_id | weight | status_id | |:----|--------------|---------|------------:| | 1 | 4 | 1.2 | 1 | | 2 | 5 | 480 | 1 |
  3. Table Three product_carton表三product_carton

     | id | product_id | weight | status_id | |:----|--------------|---------|------------:| | 1 | 4 | 10.2 | 1 | | 2 | 5 | 4.8 | 2 |

Where table product and status table as shown below:-其中表产品状态表如下所示:-

a.一种。 product table产品

    | id  |     name      |
    |:----|--------------:|
    |  4  |  Choco Cake   |
    |  5  | Hazelnut Bun  |

b.status table状态

    | id  |  description  |
    |:----|--------------:|
    |  1  |   Available   |
    |  2  |  Unavailable  |

How can I get all status from those 3 tables (product_unit, product_package & product_carton) with 1 query ?如何通过1 个查询从这 3 个表(product_unit、product_package 和 product_carton)中获取所有状态

So far, I'm able to do this:-到目前为止,我能够做到这一点:-

$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();

With above query I can get 'statusDesc' from product_unit table ONLY , since I 'leftJoin()' status table with product_unit table .通过上述查询,我只能从product_unit 表中获取'statusDesc' ,因为我使用 product_unit 表获取了 'leftJoin()' 状态表

How can I JOIN the other 2 tables (product_package & product_carton) and get the statusDesc from them also?我如何加入其他2 个表(product_package 和 product_carton)并从它们那里获取statusDesc Is there any way I can do this with 1 Query ?有什么办法可以用1 Query做到这一点吗?

$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();

PS.附注。 Maybe innerJoin will be more suitable then leftJoin?也许innerJoin会比leftJoin更合适?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM