2013-03-06 5 views
4

내 포럼에 대한 모든 카테고리를 표시하는 목록을 작성하려고합니다. ID와 함께 카운트와 함께이 카테고리에 연결된 스레드 수를 나타내는 카테고리 이름을 표시합니다.SQL 카운터 및 리턴 기가 결과를 두 배로 만듭니다.

완벽하게 작동하지만 결과가 두 번 인쇄됩니다.

다음은 SQL

SELECT categories.category_name, threads.thread_category_id, COUNT(*) 
         AS 'threadCount' FROM threads 
         INNER JOIN categories ON categories.category_id = threads.thread_category_id 
         GROUP BY categories.category_name, threads.thread_category_id 

인 결과를 enter image description here

입니다 그리고 당신이 볼 수 있듯이, 그것은 어떤이 should't, 두 번 같은 일을 인쇄합니다.

편집 : 여기에 ASP가 있습니다.

1) SQL이 잘못 아마 당신이 있습니다 (:

<asp:Repeater ID="categories" runat="server"> 
    <HeaderTemplate> 
    <table id="kategorier" cellspacing="0"> 
     <tr> 
      <td class="head">Name</td> 
      <td class="head" style="width:70px">Number of Threads</td> 
     </tr> 
    </HeaderTemplate> 
    <ItemTemplate> 
     <tr> 
      <td class="item"><a href="Kategori.aspx?id=<%# Eval("thread_category_id") %>"><%# Eval("category_name") %> - ID: <%# Eval("thread_category_id")%></a></td> 
      <td class="item" style="text-align:right"><%# Eval("threadCount") %></td> 
     </tr> 
    </ItemTemplate> 
    <FooterTemplate> 
    </table> 
    </FooterTemplate> 
</asp:Repeater> 
+0

, 중복 행 재곡이 있습니까? –

+0

질의 실행/빌더에서 실행할 때 중복되지 않습니다. –

+3

문제가 쿼리에 있지 않으므로 ASP 코드에 있습니다. –

답변

3

은 기본적으로 당신이 행을 복제 할 2 곳을 제거 DISTINCT 연산자 사용)

2) C# 코드가 잘못되었습니다 (데이터 소스를 확인해야 할 가능성이 높음)

SQL pls를 확인하십시오. C# 코드를 우리와 공유하십시오.

사용하면 MySQL을 바로 위에 해당 쿼리를 실행할 때이

SELECT distinct category_name, thread_category_id, threadCount 
FROM 
(SELECT categories.category_name, threads.thread_category_id, COUNT(*) 
         AS 'threadCount' FROM threads 
         INNER JOIN categories ON categories.category_id = threads.thread_category_id 
         GROUP BY categories.category_name, threads.thread_category_id) A 
+0

여전히 답변의 대부분이 제안했습니다. 이 방법은 작동하지 않지만 여전히 중복됩니다. –

+0

@KevinJensenPetersen MS SQL Server Management Studio에서 직접 tsql을 실행할 수 있습니까? 또한 행이 중복되었는지 확인하기 위해 데이터베이스에서 데이터를 가져올 때 행을 디버그 할 수 있습니까? –

+1

모두를 두려워하지 마라, 나는 거대한 바보 다. 내가 이것을 알아 차리지 못했기 때문에 나는 두 개의 어댑터를 가지고 있었다. 채우기 (dt); 같은 장소에있는 제 코드에서 중복이 발생했습니다. 미안합니다! –

0

를 사용하여 고유 .its 모든 중복 행 당신의 ASP가 올 경우

SELECT DISTINCT(threads.thread_category_id), categories.category_name, COUNT(*) 
         AS 'threadCount' FROM threads 
         INNER JOIN categories ON categories.category_id = threads.thread_category_id 
         GROUP BY threads.thread_category_id 
+0

여전히 중복됩니다. –

+0

이제 나는 내 post.please 체크에서 편집했습니다 –

+0

나는 이것을 알아 채지 못했기 때문에 모두를 괴롭히지 않았습니다. 나는 두 개의 어댑터를 가지고있었습니다. 채우기 (dt); 같은 장소에있는 제 코드에서 중복이 발생했습니다. 미안합니다! –

관련 문제