C# SQL计数器和返回器,将结果加倍

C# SQL计数器和返回器,将结果加倍,c#,asp.net,mysql,sql,repeater,C#,Asp.net,Mysql,Sql,Repeater,我正在尝试做一个列表,在我的论坛上显示我的所有类别。显示类别名称、ID和计数,计算有多少线程附加到此类别 它工作得很好,但是,它会打印两次结果 这就是SQL SELECT categories.category_name, threads.thread_category_id, COUNT(*) AS 'threadCount' FROM threads INNER JOIN categories

我正在尝试做一个列表,在我的论坛上显示我的所有类别。显示类别名称、ID和计数,计算有多少线程附加到此类别

它工作得很好,但是,它会打印两次结果

这就是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
结果如下

正如你所看到的,它会把同一个东西打印两次,但不应该

编辑:这是ASP

<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>

使用Distinct。删除所有重复的行

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

如果ASP正确,则基本上有两个位置可以复制行:

1) SQL错误(可能必须使用DISTINCT运算符)

2) C#代码错误(可能需要检查数据源)

请检查您的SQL。和我们分享你的C代码

用这个

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

当您直接在mysql上执行上面的查询时,是否返回了重复的行?我没有,当我在查询运行/构建器上运行它时,它不会重复。因此问题不在查询上,而是在asp代码上。我的asp中没有任何错误。将代码发布到您关联查询返回和repeater数据源的位置仍然,正如大部分答案所暗示的那样。这不起作用,仍然重复。@KevinJensenPetersen您能在MS SQL Server Management Studio中直接执行我的tsql吗?当您从数据库获取数据时,您是否可以调试该行以查看行是否重复?别担心,每个人,我都是个大傻瓜,因为我没有注意到这一点,我有两个适配器。Fill(dt);在我的代码中的相同位置,这导致了重复。对不起!别担心每个人,我是个大白痴,因为我没有注意到这一点,我有两个适配器;在我的代码中的相同位置,这导致了重复。对不起!