简体   繁体   English

SQl左联接查询

[英]SQl Left Join Query

These are the tables that i have(I got this): 这些是我有的表(我知道了):

 table building: b_id(key relation with table build-works b_id):1 2 3 
 field1: buildingA, buildingB,
 buildingC

 table build-works: b_id:1 1 2 3 3 3
 w_id: 1 2 1 1 2 3

 table works: w_id(key relation with table build-works w_id): 1 2 3 4
 field1: electricity, sanitary, shell,
 roofing

Now I want to know the works per building? 现在我想知道每个建筑物的作品吗? How can i do this with sql, and can you give my the example also with zend_db? 我该如何使用sql进行操作,也可以使用zend_db给出示例吗? Thanks 谢谢

Assuming b_id is the primary key for building, (b_id, w_id) is key for build_works, and w_id is key for works you can do it as follows: 假设b_id是构建的主键,(b_id,w_id)是build_works的键,而w_id是work的键,则可以按以下步骤进行操作:

Project_building.field1,works.field1(building JOIN build_works JOIN works) Project_building.field1,works.field1(建立JOIN build_works JOIN作品)

Note that you have to rename field1 of works to something else when doing JOIN. 请注意,在执行JOIN时,必须将作品的field1重命名为其他名称。

Also note that this might not be the most efficient way to do it. 还要注意,这可能不是最有效的方法。

using left joins since its in the title 从标题开始使用左连接

SELECT *
FROM   building b 
       LEFT JOIN buildworks bw 
         ON b.b_id = bw.b_od 
       LEFT JOIN works w 
         ON bw.w_id = w.w_id 

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

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