在此sql语句中添加另一个表

时间:2018-03-28 05:06:06

标签: mysql sql database

目前我有这个sql语句将三个表与玩家ID联合起来,但是我想改变它以便我也可以加入playerdetails表。我下面有

"SELECT technicaldetails.*,
                            mentaldetails.*,
                            physicaldetails.*
                    FROM    PlayerDetails
                    INNER JOIN mentaldetails ON mentaldetails.PlayerId = playerdetails.PlayerId 
                    INNER JOIN technicaldetails ON technicaldetails.PlayerId = playerdetails.PlayerId
                    INNER JOIN physicaldetails ON physicaldetails.PlayerId = playerdetails.PlayerId                       
                    WHERE playerdetails.playerID = " & DetailsPlayerID & ";"

PlayerDetails表

PlayerID | FirstName | Lastname
-------------------------------
1        | John      | Smith

详细信息表看起来与此类似

PlayerID | Corners | Crossing | BallControl|ect...
-------------------------------------------
1        | 15      | 13       | 19

2 个答案:

答案 0 :(得分:0)

  

您可以使用以下查询..

 SELECT technicaldetails.*, mentaldetails.*, physicaldetails.*,
 detailtable.* FROM PlayerDetails
 INNER JOIN mentaldetails ON mentaldetails.PlayerId =playerdetails.PlayerId 
 INNER JOIN technicaldetails ON technicaldetails.PlayerId = playerdetails.PlayerId
 INNER JOIN physicaldetails ON physicaldetails.PlayerId = playerdetails.PlayerId  
 left join detailtable on physicaldetails.PlayerId = detailtable.PlayerId                     
 WHERE playerdetails.playerID = " & DetailsPlayerID & ";

答案 1 :(得分:0)

您需要的就像添加新选定列一样简单。您的实际查询以

开头
SELECT 
    technicaldetails.*,
    mentaldetails.*,
    physicaldetails.*
FROM
    PlayerDetails
    INNER JOIN mentaldetails ON ...
    INNER JOIN technicaldetails ON ...
    INNER JOIN physicaldetails ON ...
WHERE
    ...

请注意,您告诉SQL引擎要查看*technicaldetailsmentaldetails表中的所有(physicaldetails)列。由于您已经引用了PlayerDetails中的FROM表,因此您只需在SELECT上添加列。

SELECT
    PlayerDetails.*,
    technicaldetails.*,
    mentaldetails.*,
    physicaldetails.*
FROM
    PlayerDetails
    INNER JOIN mentaldetails ON ...
    INNER JOIN technicaldetails ON ...
    INNER JOIN physicaldetails ON ...
WHERE
    ...

最终会像以下一样:

"SELECT 
    PlayerDetails.*,
    technicaldetails.*,
    mentaldetails.*,
    physicaldetails.*
FROM    
    PlayerDetails
    INNER JOIN mentaldetails ON mentaldetails.PlayerId = playerdetails.PlayerId 
    INNER JOIN technicaldetails ON technicaldetails.PlayerId = playerdetails.PlayerId
    INNER JOIN physicaldetails ON physicaldetails.PlayerId = playerdetails.PlayerId                       
WHERE 
    playerdetails.playerID = " & DetailsPlayerID & ";"

如果您移除WHERE,您会看到所有玩家的详细信息。

"SELECT 
    PlayerDetails.*,
    technicaldetails.*,
    mentaldetails.*,
    physicaldetails.*
FROM    
    PlayerDetails
    INNER JOIN mentaldetails ON mentaldetails.PlayerId = playerdetails.PlayerId 
    INNER JOIN technicaldetails ON technicaldetails.PlayerId = playerdetails.PlayerId
    INNER JOIN physicaldetails ON physicaldetails.PlayerId = playerdetails.PlayerId ;"