SQL-Query用于具有引用其他外键字段的外键字段的表

时间:2017-07-07 10:31:21

标签: mysql database foreign-keys relational-database foreign-key-relationship

我的数据库中有以下表格结构:

[table workers]
ID [PK] | worker | combined [FK]
--------+--------+--------------+
1       | John   |    2
--------------------------------+
2       | Adam   |    1

[table combined]
ID [PK] | name   | helper [FK]
--------+----------------------+
1       | name1  |  1
2       | name2  |  2

[table helper]
ID [PK] | department [FK] | location [FK]
--------+-------------+-------------------
1       |       2         |     3  
2       |       1         |     1

[table departments]
ID [PK] | department
--------+-------------+
1       | Development |
2       | Production  |

[table location]
ID [PK] | department
--------+--------------+
1       | Paris        |
2       | London       |
3       | Berlin       |

表“workers”具有外键字段(“组合”)。表“combined”有一个字段名和一个外键字段“helper”,它也是一个带有两个外键字段的表。

我现在的问题是,获取下表的最简单的SQL查询是什么:

[table workers]
ID [PK] | worker | combined-Name| department | location
--------+--------+--------------+------------+-----------
1       | John   |    name2     | Development|   Paris
--------------------------------+------------+-----------
2       | Adam   |    name1     | Production |   Berlin

我已经尝试了一些LEFT-JOINS,但没有管理它以获得所有“清理”到表“工人”

2 个答案:

答案 0 :(得分:1)

此查询可行:

SELECT w.ID, worker, c.name AS `combined-Name`, d.department, l.department as 
location FROM workers w
LEFT JOIN combined c ON c.ID = w.combined
LEFT JOIN helper h ON h.ID = c.helper
LEFT JOIN departments d ON d.ID = h.department
LEFT JOIN location l ON l.ID = h.location
GROUP BY w.ID

我使用AS关键字将名称设置为首选输出。

使用提供的结构和数据在本地测试。

它基本上是4个简单的左连接,然后我选择了外来表的名称列,而不是选择ID。

引用了c.name上的别名,因为我们需要转义特殊字符-

答案 1 :(得分:-1)

使用以下查询:

<table class="main-header" data-module="Header" data-bgcolor="Header" data-bg="Header" style="position: relative; opacity: 1; z-index: 0; background-image: url(https://i.ytimg.com/vi/vUQUDS-TL6Q/hqdefault.jpg); background-size: cover; background-position: 50% 40%;" background="https://i.ytimg.com/vi/vUQUDS-TL6Q/hqdefault.jpg" width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
       <tbody><tr>
         <td class="zay600" border="0" align="center" cellpadding="0" cellspacing="0">
           <table cellspacing="0" cellpadding="0" align="center" border="0" width="90%" class="zay600">
             <!-- img -->
             <tbody><tr>
               <td align="center">
                 <table align="center" class="zay600" width="600" border="0" cellspacing="0" cellpadding="0">
                   <!-- logo -->
                   <tbody><tr>
                     <td>
                       <table align="left" class="zay3-3" width="120" border="0" cellspacing="0" cellpadding="0">
                         <tbody><tr>
                           <td height="40">
                           </td>
                         </tr>
                         <!-- logo -->
                         <tr>
                           <td align="center" valign="middle" style="line-height:0px;">
                            <img style="display:block; font-size:0px; line-height:0px; border:0px;" src="http://www.dickson-constant.com/medias/images/catalogue/api/m654-grey-680.jpg" width="249" height="70" alt="img">
                          </td>
                         </tr>
                         <!-- end logo -->
                       </tbody></table>
                       <!--Space-->
                       <table width="1" height="30" border="0" cellpadding="0" cellspacing="0" align="left">
                         <tbody><tr>
                           <td height="30" style="font-size: 0;line-height: 0;border-collapse: collapse;">
                             <p style="padding-left: 24px;">
                               &nbsp;                                                                                    </p>
                           </td>
                         </tr>
                       </tbody></table>
                       <table align="right" class="zay3-3" width="280" border="0" cellspacing="0" cellpadding="0">
                         <tbody><tr>
                           <td height="50">
                           </td>
                         </tr>
                         <!-- logo -->
                         <tr>
                           <td align="center">
                             <table class="zay-inner" width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
                               <tbody><tr>
                                <td align="center" valign="middle" style="line-height:0px;"><img style="display:block; font-size:0px; line-height:0px; border:0px;" src="http://www.dickson-constant.com/medias/images/catalogue/api/m654-grey-680.jpg" width="280" height="36" alt="img">
                              </td></tr>
                             </tbody></table>
                           </td>
                         </tr>
                         <!-- end logo -->
                       </tbody></table>
                       <!--End Space-->
                     </td>
                   </tr>
                   <!-- menu -->
                   <!-- end menu -->
                 </tbody></table>
               </td>
             </tr>
             <!-- end logo -->
             <tr>
               <td height="100" class="header-td">
               </td>
             </tr>
             <!-- subtitle -->
             <tr>
               <td>
                 <!-- content -->
                 <table class="zay600" width="600" border="0" align="center" cellpadding="0" cellspacing="0">
                   <tbody><tr>
                     <td align="center">
                       <table border="0" width="184" align="center" cellpadding="0" cellspacing="0" class="container580">
                         <tbody><tr>
                           <td align="center" valign="middle" style="line-height:0px;">
                             
                           </td>
                         </tr>
                       </tbody></table>
                     </td>
                   </tr>
                   
                   <tr>
                     <td align="center">
                       <!-- img -->
                       <table align="center" class="zay3-3" width="275" border="0" cellspacing="0" cellpadding="0">
                         <tbody><tr>
                           <td align="center" style="line-height:0px;">
                             
                           </td>
                         </tr>
                       </tbody></table>
                       <!-- end img -->
                     </td>
                   </tr>
                   
                   <tr>
                    <td align="center">
                      <!-- img -->
                      <table align="center" class="zay3-3" width="275" border="0" cellspacing="0" cellpadding="0">
                        <tbody><tr>
                          <td align="center" style="line-height:0px;">
                            
                          </td>
                        </tr>
                      </tbody></table>
                      <!-- end img -->
                    </td>
                  </tr>                 
                  
                  
                 </tbody></table>
                 <!-- end content -->
               </td>
             </tr>
             <!-- end subtitle -->
             <tr>
               <td height="100" class="header-td">
               </td>
             </tr>            
             
           </tbody></table>
         </td>
       </tr>
     </tbody></table>