简体   繁体   中英

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? 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:

Project_building.field1,works.field1(building JOIN build_works JOIN works)

Note that you have to rename field1 of works to something else when doing JOIN.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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