简体   繁体   English

MySQL INNER JOIN 查询问题

[英]MySQL INNER JOIN query issue

I have created an INNER JOIN query as shown below and was wondering how I can make it work?我创建了一个 INNER JOIN 查询,如下所示,想知道如何使它工作? I need for the HomeTeam and AwayTeam to equal TeamID in the query.我需要 HomeTeam 和 AwayTeam 在查询中等于 TeamID。 Any help would be much appreciated.任何帮助将非常感激。 Thanks谢谢

$result = mysqli_query($con,"SELECT results.*,team.TeamName 
                             FROM results 
                                INNER JOIN team ON team.TeamID = results.HomeTeam 
                                INNER JOIN team on team.TeamID = results.AwayTeam");

You need to use aliases for the table that you are including twice.您需要为包含两次的表使用别名。 Otherwise mysql cannot distinguish between the two.否则mysql无法区分两者。

To be able to process the results easily, you can do the same with the names you are selecting.为了能够轻松处理结果,您可以对选择的名称执行相同操作。

Something like:就像是:

SELECT 
    results.*, 
    t1.TeamName AS TeamNameHome,
    t2.TeamName AS TeamNameAway
FROM results 
INNER JOIN team t1
    ON t1.TeamID = results.HomeTeam 
INNER JOIN team t2
    ON t2.TeamID = results.AwayTeam

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM