2016-05-08 82 views
1

我有一個表設置爲每個數據項具有唯一的Match_ID。但是,當我想調用從Team表中收集團隊名稱的自定義選擇查詢時,我得到的重複行列出了兩個相同的Match_ID。我想在GridView中以列Home teamAway 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> 

enter image description here

enter image description here

總之,如何使這個查詢的每一行返回唯一一個獨特MATCH_ID

+1

只是說明:你的'WHERE EXISTS'部分什麼都不做。如果表爲空,它將返回FALSE(0)。否則它總是回退TRUE(1)。 –

回答

2

那不是......

SELECT s.Match_ID 
    , s.Home_team_ID 
    , s.Away_team_ID 
    , s.Home_team_score 
    , s.Away_team_score 
    , s.Game_date 
    , h.Team_name home_team 
    , a.Team_name away_team 
    FROM MatchStatistics s 
    JOIN Team h 
    ON h.Team_ID = s.Home_team_ID 
    JOIN Team a 
    ON a.Team_ID = s.Away_team_ID 

???

+0

啊輝煌謝謝你!我不知道你可以這樣做 –

+0

現在唯一的問題是更新GridView。我在主隊和客隊ID列上製作了DropDownLists,這些列顯示了他們的球隊名稱,每個ID都設置了值。然而無論如何,由數據源提供的更新查詢說'參數化查詢「(@Match_ID INT,@ Home_team_ID INT,@ Away_team_ID INT,@ Home_team_sc」需要參數「@Match_ID」,這是不supplied.' 我有參數化Match_ID,所以我不知道爲什麼我得到這個錯誤 –

+0

我不知道ASP,所以我懷疑我可以幫助 - 但我非常確信查詢不是問題。 – Strawberry