我有一個表設置爲每個數據項具有唯一的Match_ID
。但是,當我想調用從Team
表中收集團隊名稱的自定義選擇查詢時,我得到的重複行列出了兩個相同的Match_ID
。我想在GridView中以列Home team
,Away team
,Home team score
,和Game date
顯示唯一的行。我不想在他們的專欄中顯示團隊的ID,而是他們的團隊名稱。但是我也想確保我可以更新這個GridView,因爲使用AS阻止了我在過去能夠這樣做。從自定義SQL選擇查詢刪除重複的行
下面是我用圖像顯示查詢返回的內容。我正在使用SQL數據源。
SELECT MatchStatistics.Match_ID, MatchStatistics.Home_team_ID, MatchStatistics.Away_team_ID, MatchStatistics.Home_team_score, MatchStatistics.Away_team_score, MatchStatistics.Game_date,
Team.Team_name
FROM MatchStatistics INNER JOIN
Team ON MatchStatistics.Home_team_ID = Team.Team_ID OR MatchStatistics.Away_team_ID = Team.Team_ID
WHERE EXISTS
(SELECT DISTINCT Match_ID
FROM MatchStatistics AS MatchStatistics_1)
<asp:GridView ID="EnterMatchGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="Match_ID" DataSourceID="SqlDataSource4" OnRowUpdating="EnterMatchGridView_RowUpdating" OnRowDeleting="EnterMatchGridView_RowDeleting">
<Columns>
<asp:BoundField DataField="Match_ID" HeaderText="Match_ID" SortExpression="Match_ID" InsertVisible="False" ReadOnly="True" />
<asp:TemplateField HeaderText="Home Team" SortExpression="Home_Team_Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Home_Team_Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Home_Team_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Away Team" SortExpression="Away_Team_Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Away_Team_Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Away_Team_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Home Team Score" SortExpression="Home_team_score">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Home_team_score") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Home_team_score") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Away Team Score" SortExpression="Away_team_score">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Away_team_score") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Away_team_score") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" SortExpression="Game_date">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Game_date", "{0:dd-MM-yyyy}") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Game_date", "{0:dd-MM-yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit/Delete">
<ItemTemplate>
<asp:LinkButton ID="BtnEdit" runat="server" CausesValidation="false" CommandName="Edit" Text="Edit" />
<span onclick="return confirm ('Are you Sure?')">
<asp:LinkButton ID="BtnDelete" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" />
</span>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="BtnUpdate" runat="server" CausesValidation="true" CommandName="Update" ConflictDetection="OverwriteChanges" Text="Update" ValidationGroup="EnterMatchGridView" />
<asp:Button ID="BtnCancel" runat="server" CausesValidation="false" CommandName="Cancel" ConflictDetection="OverwriteChanges" Text="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
總之,如何使這個查詢的每一行返回唯一一個獨特MATCH_ID
?
只是說明:你的'WHERE EXISTS'部分什麼都不做。如果表爲空,它將返回FALSE(0)。否則它總是回退TRUE(1)。 –