Search code examples
asp.netsqlaspxgridview

One to many relationship not working in Gridview?


How am I manage to get the Countries in the same row? Is Gridview the wrong way to go? I just want to print out some matches and show the teams, matchtime and matchID... Relationship ! This is the outcome: gridview_results !


Solution

  • You have to join table Countries twice with different alias names:

    SELECT M.MatchID,M.Matchtime,C1.Country as HomeTeam, C1.CountryID as HomeTeamID, C1.Group_ID as HomeGroupID,C2.Country as AwayTeam, C2.CountryID as AwayTeamID, C2.Group_ID as AwayGroupID
    FROM Matches M JOIN
         Countries C1 ON M.HomeTeam_ID=C1.CountryID JOIN
         Countries C2 ON M.AwayTeam_ID=C2.CountryID
    

    Explanation:

    Here Countries table is joined with alias names C1 and C2.

    C1 represents the country details of home team. C2 represents the country details of away team.