<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="Match_ID" DataSourceID="SqlDataSource4">
<Columns>
<asp:BoundField DataField="Team_name" HeaderText="Team_name" SortExpression="Team_name" />
<asp:BoundField DataField="Match_ID" HeaderText="Match_ID" InsertVisible="False" ReadOnly="True" SortExpression="Match_ID" />
<asp:BoundField DataField="Home_team_ID" HeaderText="Home_team_ID" SortExpression="Home_team_ID" />
<asp:BoundField DataField="Away_team_ID" HeaderText="Away_team_ID" SortExpression="Away_team_ID" />
<asp:BoundField DataField="Home_team_score" HeaderText="Home_team_score" SortExpression="Home_team_score" />
<asp:BoundField DataField="Away_team_score" HeaderText="Away_team_score" SortExpression="Away_team_score" />
<asp:BoundField DataField="Game_date" HeaderText="Game_date" SortExpression="Game_date" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:SportsData2ConnectionString %>" SelectCommand="SELECT Team.Team_name, MatchStatistics.Match_ID, MatchStatistics.Home_team_ID, MatchStatistics.Away_team_ID, MatchStatistics.Home_team_score, MatchStatistics.Away_team_score, MatchStatistics.Game_date FROM MatchStatistics INNER JOIN Team ON MatchStatistics.Home_team_ID = Team.Team_ID AND MatchStatistics.Away_team_ID = Team.Team_ID ORDER BY MatchStatistics.Game_date DESC"></asp:SqlDataSource>
我想创建一个显示MatchStatistics内所有记录的GridView。为此,我需要从Team_name
表中收集Team
。但是,即使MatchStatistics
有数据,select语句也不起作用。它有什么问题?
我想要的GridView应该是MatchStatistics
的所有内容,主页和远程团队ID已替换为Team_names
。与下面类似,但有下拉列表代替主队和客队ID。
答案 0 :(得分:0)
我想你想用OR:
FROM MatchStatistics
INNER JOIN Team
ON MatchStatistics.Home_team_ID = Team.Team_ID
**OR** MatchStatistics.Away_team_ID = Team.Team_ID
实际上,你期待球队与主场比赛相匹配,我想你想找到任何主场或客场球队。
答案 1 :(得分:0)
尝试此查询:
SELECT
t1.Team_name AS Home_Team_Name,
t2.Team_name AS Away_Team_Name,
m.Match_ID,
m.Home_team_score,
m.Away_team_score,
m.Game_date
FROM MatchStatistics m
INNER JOIN Team t1 ON t1.Team_ID = m.Home_team_ID
INNER JOIN Team t2 ON t2.Team_ID = m.Away_team_ID
ORDER BY m.Game_date DESC
这应该给你团队名而不是teamId。
请记住删除GridView中的第一个列并更新DataFields和SortExpressions以匹配查询中的新列名称:
Match_ID
Home_Team_Name
Away_Team_Name
Home_team_score
Away_team_score
Game_date
您还可以查看sqlfiddle (MySQL)我做的测试。只要知道我在小提琴中使用了一些稍微不同的列名,如果你觉得需要从中复制一些东西。