左外连接在3个表中

时间:2012-07-14 21:04:18

标签: mysql join left-join outer-join

我有3张桌子 车间和车主
shop是父表,其他2是子表

SHOP    
s_id, floor_id,s_owner,s_remarks
1,2,1,big shop   
2,2,3,near bank   
3,1,2,corner   
4,7,7,FAKE FLOOR AND OWNER   

OWNERS   
o_id, o_name, contact
1,gale,009659999999   
2,smith,00447676767   
3,pathan,0088787878   

FLOORS   
f_id, f_name
1,FIRST FLOOR   
2,SECOND FLOOR  
3,THIRD FLOOR   


select shop.s_id, floors.f_name, owners.o_id, s_remarks  
from ? join ? 

显示所有店铺记录,即使在业主或楼层没有相关记录

1 个答案:

答案 0 :(得分:3)

正如您在标题中所建议的那样,您需要使用LEFT JOIN。这应该让你开始:

SELECT shop.s_id, floors.f_name, owners.o_id, s_remarks
FROM ?
LEFT JOIN ? ON ? = ?
LEFT JOIN ? ON ? = ?

您只需要使用正确的表名或列名替换问号。