从具有相同列名的不同表中选择/加入多个字段

时间:2016-07-08 17:40:10

标签: mysql join left-join inner-join outer-join

我有一个密钥表,其中包含多个具有相同列名的数据表,以及一个用户表。我正在尝试从密钥表中选择值,将此输出与每个数据表中的一些选定值通过唯一ID(uaID)连接到已从密钥表中选择的条目(每个密钥表结果将具有相对的连接只有一个数据表范围,不是所有数据表和一些条目将返回数据表中的空结果,我们不希望这会破坏任何东西或因为null结果而被省略,然后最后将一些用户数据连接到users表中的每个结果。这将始终有结果。

让我"画"我的表格的基本版本,你可以看到。

 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表的调用结束时始终显示正常。很明显,数据表中匹配的字段名称存在问题。我没有得到任何错误,并且该过程完成,只是缺少所提到的数据。我试过了不同的JOINS - INNER JOIN,OUTER JOIN,LEFT OUTER JOIN。显然必须有一种方法来实现这一点,但似乎无法在网上找到任何关于这个特定问题的参考。有人可以告诉我,我做错了吗?

2 个答案:

答案 0 :(得分:2)

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

$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表中的数据合并到同一列的另一种方法是使用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

答案 1 :(得分:0)

你必须使用别名

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

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