首先,我过度简化了我的表格,只是为了提出问题的重点。我有两张桌子,第一张是“Clubs”,第二张是“Fixtures”
分会数据库有club_ID和club_Name 灯具有Fixt_Date,然后是主队和客队的ID号。
如何显示显示日期的查询,然后显示两个俱乐部的名称?
SELECT Fixtures.Match_Date AS Date, Clubs.Club_Name AS Home, Clubs.Club_Name AS AWAY
FROM Fixtures INNER JOIN Clubs ON Fixtures.Away = Clubs.ID AND Fixtures.Home = Clubs.ID;
我有一种感觉,这是非常错误的,但我对SQL没有任何经验。
答案 0 :(得分:0)
尝试使用IN
运算符..
SELECT F.Match_Date AS Date,
Max(case when C.ID = F.Home then C.Club_Name end) AS Home,
Max(case when C.ID = F.Away then C.Club_Name end) AS AWAY
FROM Fixtures F
INNER JOIN Clubs C ON C.ID in (F.Away, F.Home);
Group by F.Match_Date
只有当Away
和Home
包含相同的分会Id
答案 1 :(得分:0)
试试这个:
SELECT Fixtures.Match_Date AS Date,
hc.Club_Name AS Home,
ac.Club_Name AS AWAY
FROM Fixtures
INNER JOIN
Clubs ac
ON Fixtures.Away = ac.ID
INNER JOIN
Clubs hc
ON Fixtures.Home = hc.ID;
答案 2 :(得分:0)
如果你想要的只是获得俱乐部的名字而不是ID,你也可以使用子选择:
SELECT Match_Date AS Date,(SELECT Club_Name FROM Clubs WHERE ID = Home)AS Home,(SELECT Club_Name FROM Clubs WHERE ID = Away)远离灯具
注意:使用此解决方案,clubs_id必须是PK。