在一个SQL中显示两行

时间:2018-02-12 00:29:17

标签: sql database ms-access select

我的DataBase中有三个表名为Equipe(团队),Match(Match),Jouer(Played),我试图用这个来显示联盟的结果:

SELECT DISTINCT M.ID_MATCH, NOM_EQUIPE, BUT_MARQUE
FROM EQUIPE AS E, JOUER AS J, [MATCH] AS M
WHERE E.ID_EQUIPE = J.ID_EQUIPE and J.ID_MATCH = M.ID_MATCH
ORDER BY M.ID_MATCH;

,表格如下:

EQUIPE:

enter image description here

匹配度:

enter image description here

它们与tabble JOUER相关:

enter image description here

问题是,我得到的结果如下:

enter image description here

有没有办法让结果更像

 ID_MATCH , Team1 , result1 , Team2 , result2

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT M.ID_MATCH, E1.NOM_EQUIPE, J1.BUT_MARQUE, E2.NOM_EQUIPE, J2.BUT_MARQUE
FROM JOUER AS J1
INNER JOIN JOUER AS J2 ON J2.ID_MATCH = J1.ID_MATCH and J2.ID_EQUIPE > J1.ID_EQUIPE 
INNER JOIN [MATCH] AS M ON J1.ID_MATCH = M.ID_MATCH
INNER JOIN EQUIPE AS E1 ON E1.ID_EQUIPE = J1.ID_EQUIPE
INNER JOIN EQUIPE AS E2 ON E2.ID_EQUIPE = J2.ID_EQUIPE
ORDER BY M.ID_MATCH; 

诀窍是在ID_MATCH上加入JOUER,但也只有在J2.ID_EQUIPE> J1.ID_EQUIPE。这样你只能获得1个团队组合而不是4个团队。

我希望这会有所帮助。 Bonne Chance!