MYSQL查询显示客队名称和主队名称

标签 mysql

我有两张 table 。
游戏包括 Date、hteam、ateam、hscore、ascore。
team 包括 id、tname。

此查询:

SELECT
    games.DATE,
    teams.tname AS "Away",
    games.ascore AS "A Score",
    games.hteam AS "Home",
    games.hscore AS "H Score"
FROM
    games
INNER JOIN teams ON games.ateam = teams.id
LEFT OUTER JOIN teams AS t
ON
    games.hteam = t.tname
ORDER BY
    games.DATE ASC

最终产生以下输出:

DATE Ascending 1        Away        A Score Home    H Score     
2008-01-01 20:00:00     Saxons      2       3       0
2008-01-01 20:00:00     Spurs       0       1       1
2008-01-08 20:00:00     Saxons      1       2       2
2008-01-08 20:00:00     Celtics     1       1       1

我的问题是如何让主队显示为其名称,而不是其 ID,因此输出为:

DATE Ascending 1        Away        A Score Home        H Score     
2008-01-01 20:00:00     Saxons      2       Celtics     0
2008-01-01 20:00:00     Spurs       0       Wanderers   1
2008-01-08 20:00:00     Saxons      1       Spurs       2
2008-01-08 20:00:00     Celtics     1       Wanderers   1

最佳答案

您需要与“teams”表再次连接才能获得结果。 所以类似的事情:

SELECT
    games.DATE,
    teams_away.tname AS "Away",
    games.ascore AS "A Score",
    teams_home.tname AS "Home",
    games.hscore AS "H Score"
FROM
    games
INNER JOIN teams teams_away ON games.ateam = teams_away.id
INNER JOIN teams teams_home ON games.hteam = teams_home.id
ORDER BY
    games.DATE ASC

关于MYSQL查询显示客队名称和主队名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55263459/

相关文章:

php/mysql 列表函数不返回任何内容!帮助!

mysql - JOIN 遗漏了重复数据行。子查询工作正常

php - 拉维尔 |仅在范围内覆盖默认 DB::connection 一次

MySQL - 仅获取最近的前一条记录,但不获取所有前一条记录

列名中的mysql括号

mysql - 我应该为越南语、俄语和英语数据库使用什么 mysql 排序规则

mysql - 外键存在,我得到 MySQL 错误 : 1452: Cannot add or update a child row: a foreign key constraint fails

mysql - 在报告模块中获取分配项目超过 2 个的联系人

php - 创建一个每周数组,每天包含多个条目

mysql - 将 PHP 数组移植到 MySQL 模式以获得 ACL 授权