SQL计数器和返回器,双打的结果 [英] SQL Counter and returner, doubles the result
问题描述
我试图做一个列表,显示在我的论坛我的所有类别的。显示该类别名称,一个ID,藏汉作为计数,计数多少线程连接到这一类。
它完美,但是,它打印两次结果
这是SQL
SELECT categories.category_name,线程.thread_category_id,COUNT(*)
从线程
INNER JOIN类别ON categories.category_id = threads.thread_category_id
GROUP BY categories.category_name经纬,threads.thread_category_id
下面是结果
和,你可以看到,它打印同样的事情两次,它should't。
编辑:这里是ASP
< ASP:直放站ID =类别=服务器>
<&HeaderTemplate中GT;
<表ID =kategorierCELLSPACING =0>
< TR>
< TD类=头>名称和LT; / TD>
< TD类=头的风格=宽度:70像素>&线程LT的数目; / TD>
< / TR>
< / HeaderTemplate中>
<&ItemTemplate中GT;
< TR>
将; TD类=项>&下; A HREF =Kategori.aspx的id =&下;%#评估和演示(thread_category_id)%>中与GT;&下;%#评估和演示(CATEGORY_NAME )%> - ID:<%#的eval(thread_category_id)%>< / A>< / TD>
< TD类=项的风格=文本对齐:右><%#的eval(经纬)%>< / TD>
< / TR>
< / ItemTemplate中>
< FooterTemplate>
< /表>
< / FooterTemplate>
< / ASP:直放站>
基本上,你有2个地方,如果你的ASP重复行是正确的:
1),SQL是错误的(也许你就必须使用DISTINCT运算符)
2)C#编码是错误的(也许你要检查你的数据源)
检查您的SQL请。并与我们分享您的C#代码。
使用此
SELECT不同CATEGORY_NAME,thread_category_id,THREADCOUNT
起价
(SELECT categories.category_name,threads.thread_category_id,COUNT(*)
从线程$ b $'经纬'b INNER JOIN类别ON categories.category_id = threads.thread_category_id
GROUP BY categories.category_name,threads.thread_category_id)在
I'm trying to make a list, showing all of my Categories on my Forum. Showing the Category name, with an ID, aswell as a count, counting how many Threads is attached to this Category.
It works perfectly, however, it prints the results twice.
This is the 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
Here is the result
And as you can see, it prints the same thing twice, which it should't.
EDIT: Here is the 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>
Basically you have 2 places to duplicate rows if your ASP is correct:
1) SQL is wrong (Probably you have to use DISTINCT operator)
2) C# code is wrong (Probably you have to check your data source)
Check your SQL pls. And share your C# code with us.
Use this
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
这篇关于SQL计数器和返回器,双打的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!