Select/Join 来自不同表的具有相同列名的多个字段

Select/Join multiple fields from different tables with same column name

我有一个键table,一些数据table,其中有相同的列名,还有一个用户table。我正在尝试 select 来自键 table 的值,通过唯一 ID (uaID) 将此输出与来自每个数据 table 的一些 selected 值连接到条目 select 已经从键 table 编辑(每个键 table 结果将只有一个数据范围 table 的相对连接,而不是所有数据和一些条目将 return 来自数据 tables 的空结果,我们不希望它破坏任何东西或因为空结果而被省略),然后最后将一些用户数据加入每个结果来自用户 table。这样总会有结果的。

让我 "draw" 我的 table 的基本版本,这样你就可以看到。

 keyTable
-----------------------------------------
|  uaID  |  userID  |  key  |  appName  |
|---------------------------------------|
|  1     |    7     | ABC01 | Physics   |
|  2     |    9     | DEF20 | Geometry  |
|  3     |    12    | XJG14 | Biology   |
|  4     |    19    | DAF09 | Chemistry |
|  5     |    27    | KYT78 | Algebra   |
|  6     |    29    | PLF43 | Statistics|
|  7     |    34    | COG89 | Geology   |
|  8     |    45    | HYL72 | Art       |
|  9     |    48    | TSK45 | History   |
|  10    |    53    | BBS94 | GeoChem   |
|  11    |    59    | DOD10 | BioChem   |
|  12    |    27    | HKV62 | Music     |
-----------------------------------------

 dataTable01
-----------------------------------------------
|  uaID  |  sector  |  subSector  |   topic   |
|---------------------------------------------|
|  2     |  circle  |  volumn     |  measure  |
|  7     | triangle | hypotenuse  |pythagoras |
|  8     | square   |             |           |
|  11    | triangle | hypotenuse  |pythagoras |
-----------------------------------------------

 dataTable02
---------------------
|  uaID  |   topic  |
|-------------------|
|  1     |   door   |
|  3     |  window  |
|  9     |  porch   |
|  12    |          |
---------------------

 dataTable03
-----------------------------------------------
|  uaID  |  sector  |  subSector  |   topic   |
|---------------------------------------------|
|  4     |   cat    |   feline    |   kitty   |
|  5     |   dog    |   canine    |   rover   |
|  6     | kangaroo |  marsupial  |   jack    |
|  10    |  bunny   |  leporidae  |   peter   |
-----------------------------------------------

 users
------------------------------------------------------------------------
|  userID  |  Title  |  firstName  |  lastName  |  email               |
|----------------------------------------------------------------------|
|    7     |   Dr    |  Melissa    |  Smith     |  mel@email.com       |
|    9     |   Mr    |  Bob        |  Andrews   |  bob@email.com       |
|    12    |   Miss  |  Clare      |  Greco     |  clare@email.com     |
|    19    |   Mr    |  Dan        |  Fonseca   |  dan@email.com       |
|    27    |   Mr    |  Matt       |  Jones     |  matt@email.com      |
|    29    |   Mr    |  Chris      |  Nimmo     |  chris@email.com     |
|    34    |   Mrs   |  Lisa       |  Araujo    |  lisa@email.com      |
|    45    |   Miss  |  Raquel     |  Bailey    |  raquel@email.com    |
|    48    |   Dr    |  Steven     |  Dowd      |  steven@email.com    |
|    53    |   Prof  |  Roger      |  Hesp      |  roger@email.com     |
|    59    |   Prof  |  Sally      |  Bryce     |  sally@email.com     |
|    65    |   Mrs   |  Elena      |  Eraway    |  elena@email.com     |
------------------------------------------------------------------------

这就是我想要达到的最终结果:

-------------------------------------------------------------------------------------------------------------------------------
|  uaID  |  key  |  appName  |  sector  |  subSector  |   topic   |  title  |  firstName  |  lastName  |  email               | 
|-----------------------------------------------------------------------------------------------------------------------------|
|  1     | ABC01 | Physics   |          |             |   door    |   Dr    |  Melissa    |   Smith    |  mel@email.com       |
|  2     | DEF20 | Geometry  |  circle  |  volumn     |  measure  |   Mr    |  Bob        |   Andrews  |  bob@email.com       |
|  3     | XJG14 | Biology   |          |             |  window   |  Miss   |  Clare      |   Greco    |  clare@email.com     |
|  4     | DAF09 | Chemistry |   cat    |   feline    |   kitty   |   Mr    |  Dan        |  Fonseca   |  dan@email.com       |
|  5     | KYT78 | Algebra   |   dog    |   canine    |   rover   |   Mr    |  Matt       |  Jones     |  matt@email.com      |
|  6     | PLF43 | Statistics| kangaroo |  marsupial  |   jack    |   Mr    |  Chris      |  Nimmo     |  chris@email.com     |
|  7     | COG89 | Geology   | triangle | hypotenuse  |pythagoras |   Mrs   |  Lisa       |  Araujo    |  lisa@email.com      |
|  8     | HYL72 | Art       | square   |             |           |   Miss  |  Raquel     |  Bailey    |  raquel@email.com    |
|  9     | TSK45 | History   |          |             |   porch   |   Dr    |  Steven     |  Dowd      |  steven@email.com    |
|  10    | BBS94 | GeoChem   |  bunny   |  leporidae  |   peter   |   Prof  |  Roger      |  Hesp      |  roger@email.com     |
|  11    | DOD10 | BioChem   | triangle | hypotenuse  |pythagoras |   Prof  |  Sally      |  Bryce     |  sally@email.com     |
|  12    | HKV62 | Music     |          |             |           |   Mr    |  Matt       |  Jones     |  matt@email.com      |
-------------------------------------------------------------------------------------------------------------------------------

我试图通过执行来实现这一点:

$sql = "SELECT keyTable.uaID, keyTable.userID, keyTable.key, 
                keyTable.appName, dataTable01.sector, dataTable01.subSector, 
                dataTable01.topic, dataTable02.topic, dataTable03.sector, 
                dataTable03.subSector, dataTable03.topic, users.title, 
                users.firstName, users.lastName, users.email 
        FROM keyTable 
        LEFT OUTER JOIN dataTable01 ON keyTable.uaID = dataTable01.uaID 
        LEFT OUTER JOIN dataTable02 ON keyTable.uaID = dataTable02.uaID 
        LEFT OUTER JOIN dataTable03 ON keyTable.uaID = dataTable03.uaID 
        LEFT OUTER JOIN users ON keyTable.userID = users.userID";

我得到了所有 keyTable 数据。我把所有的用户数据都放在应该连接的地方,一切正常。我也得到了所有 dataTable03 数据,但我没有从 dataTable01dataTable02 得到任何数据显示在结果中。如果我省略对 dataTable03 的调用,那么我会从 dataTable02 中获取所有相关数据,但不会从 dataTable01 中获取数据。对 users table 的调用在最后并且总是显示正常。很明显,这是数据 table 中匹配字段名称的问题。我完全没有收到任何错误并且该过程已完成,只是缺少提到的数据。我尝试了不同的 JOINS - INNER JOIN、OUTER JOIN、LEFT OUTER JOIN。显然必须有一种方法来实现这一点,但似乎无法在网络上找到任何关于这个特定问题的参考。有人可以告诉我我做错了什么吗?

加入后,您可以使用 COALESCE 从具有匹配行的 table 中获取非空值。

$sql = "SELECT k.uaID, k.userID, k.key, k.appName, 
                COALESCE(d1.sector, d3.sector, '') AS sector, 
                COALESCE(d1.subSector, d3.subSector, '') AS subSector, 
                COALESCE(d1.topic, d2.topic, d3.topic, '') AS topic,
                users.title, users.firstName, users.lastName, users.email 
        FROM keyTable AS k
        LEFT OUTER JOIN dataTable01 AS d1 ON k.uaID = d1.uaID 
        LEFT OUTER JOIN dataTable02 AS d2 ON k.uaID = d2.uaID 
        LEFT OUTER JOIN dataTable03 AS d3 ON k.uaID = d3.uaID 
        LEFT OUTER JOIN users ON k.userID = users.userID
        ORDER BY k.uaID";

另一种将 datatablesNN table 中的数据合并到同一列的方法 os tp 使用 UNION.

SELECT k.uaID, k.userID, k.key, k.appName, IFNULL(d.sector, '') AS sector, IFNULL(d.subSector, '') AS subSector, IFNULL(d.topic, '') AS topic,
        u.title, u.firstName, u.lastName, u.email
FROM keyTable AS k
LEFT OUTER JOIN (
    SELECT uaID, sector, subSector, topic
    FROM dataTable01
    UNION
    SELECT uaID, NULL, NULL, topic
    FROM datatable02
    UNION
    SELECT uaID, sector, subSector, topic
    FROM datatable03) AS d
ON k.uaID = d.uaID
LEFT JOIN users AS u ON u.userID = k.userID
ORDER BY k.uaID

DEMO

你必须使用别名

这里有类似的问题和解决方案:

php-mysql-how-to-resolve-ambiguous-column-names-in-join-operation

select * from common inner join (
(select link from table1)
union
(select link from table2)
) as unionT
on unionT.link = common.link