有点难,select count(字段)as num 的问题。

Ray_Adon 2009-07-17 07:18:11

Sql="select top 18 zz,count(zz) as num from news where sh=1 and zz in (select user from admin where len(username)>0 and user<>'admin') group by zz order by count(zz) DESC "

上面的语句是对 [news] 和 [admin] 进行筛选统计,但不能够显示不出 rs("num")=0 的记录,就是在[admin]里有用户ID并没有在[news]中发文章,但是也要求显示出来,为了做一个发文排行的功能。

[news] //文章列表
zz //用户ID

[admin] //用户列表
user //用户ID
username //用户名

发文排行是这样
排行 用户名 发文数
1 userA 99
2 userB 88
3 userC 77
4 userD 66

但是上面SQL语句,只显示有发文数的。要怎么写 才能显示,发文数为0的。
如:
5 userE 0
6 userF 0
..........

我百度了一条类似的语法给各位大大参考,我很遗憾没从中参悟出来我想要的结果。
select AlbumName As N'相册',Count(PhotoID) As N'相片数' from T_Album left outer join (select PhotoID,PhotoName,AlbumID from T_Photo) T on T_Album.AlbumID=T.AlbumID Group By AlbumName
...全文
2254 23 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
子陌红尘 2009-07-18
  • 打赏
  • 举报
回复
如果只有ISNULL()的问题,替换成IIF()。
Ray_Adon 2009-07-18
  • 打赏
  • 举报
回复
21楼
select top 18 [user],IIF(count(zz),0) as num from news right join (select * from admin where len(username)>0 and user <>'admin') t on news.zz=t.user group by zz order by count(zz) DESC

报错信息:
用于函数参数的个数不对 在查询表达式 'isnull(count(zz),0)' 中。

把isnull()换成IIF()
报错信息:
试图执行的查询中不包含作为合计函数一部分的特定表达式 'user' 。

Ray_Adon 2009-07-18
  • 打赏
  • 举报
回复
21楼
select top 18 [user],IIF(count(zz),0) as num from news right join (select * from admin where len(username)>0 and user <>'admin') t on news.zz=t.user group by zz order by count(zz) DESC

报错信息:
用于函数参数的个数不对 在查询表达式 'isnull(count(zz),0)' 中。

把isnull()换成IIF()
报错信息:
试图执行的查询中不包含作为合计函数一部分的特定表达式 'user' 。

feixianxxx 2009-07-18
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 ray_adon 的回复:]
引用 7 楼 feixianxxx 的回复:
SQL codeselecttop18 t.user ,count(zz)as numfrom newsrightjoin (select*from adminwherelen(username)>0anduser <>'admin') ton news.zz=t.usergroupby zzorderbycount(zz)DESC试试


麦蒂大大,报错:试图执行的查询中不包含作为合计函数一部分的特定表达式 'user' 。Microsoft JET Database Engine 错误 '80040e21'


[/Quote]


这样?
select top 18 [user],isnull(count(zz),0) as num 
from news right join (select * from admin where len(username)>0 and user <>'admin') t on news.zz=t.user
group by zz order
by count(zz) DESC
Ray_Adon 2009-07-18
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 libin_ftsafe 的回复:]
try:

SQL codeselecttop18*from
(selectuser,0as numfrom
adminwherelen(username)>0anduser<>'admin'andusernotin(selectdistinct zzfrom news)unionallselect
zz,count(zz)as numfrom
newswhere
sh=1and zzin (selectuserfrom adminwherelen(username)>0anduser<>'admin')groupby zz)as torderby
t.numdesc
[/Quote]


报错:ADODB.Recordset 错误 '800a0cc1'
在对应所需名称或序数的集合中,未找到项目。
Ray_Adon 2009-07-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 feixianxxx 的回复:]
SQL codeselecttop18 t.user ,count(zz)as numfrom newsrightjoin (select*from adminwherelen(username)>0anduser<>'admin') ton news.zz=t.usergroupby zzorderbycount(zz)DESC试试
[/Quote]

麦蒂大大,报错:试图执行的查询中不包含作为合计函数一部分的特定表达式 'user' 。Microsoft JET Database Engine 错误 '80040e21'

lihan6415151528 2009-07-18
  • 打赏
  • 举报
回复
ISNULL()==IIF()。
叶子 2009-07-18
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 appleller 的回复:]
好像也不支持连接
[/Quote]

不是吧?
x2xnet 2009-07-18
  • 打赏
  • 举报
回复
先列出>0的再列出==0的,用不同WHERE条件
claro 2009-07-18
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 libin_ftsafe 的回复:]
如果只有ISNULL()的问题,替换成IIF()。
[/Quote]
appleller 2009-07-17
  • 打赏
  • 举报
回复
好像也不支持连接
--小F-- 2009-07-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 lihan6415151528 的回复:]
主要就是用上ISNULL函数即可
[/Quote]
ACCESS仿佛不支持isnull
lihan6415151528 2009-07-17
  • 打赏
  • 举报
回复
主要就是用上ISNULL函数即可
qqqwwwqw 2009-07-17
  • 打赏
  • 举报
回复
select
top 18 a.[user],count(n.zz) as num
from
admin a
left join
news n
on
a.[user]=n.zz and n.sh=1
and
len(a.username)>0 and a.[user] <>'admin'
group
by [user]
order by
count(n.zz) DESC
子陌红尘 2009-07-17
  • 打赏
  • 举报
回复
try:


select
top 18 *
from
(select
user,0 as num
from
admin
where
len(username)>0 and user <>'admin'
and
user not in(select distinct zz from news)
union all
select
zz,count(zz) as num
from
news
where
sh=1 and zz in (select user from admin where len(username)>0 and user <>'admin') group by zz) as t
order by
t.num desc
feixianxxx 2009-07-17
  • 打赏
  • 举报
回复
select top 18 t.user ,count(zz) as num 
from news right join (select * from admin where len(username)>0 and user <>'admin') t on news.zz=t.user
group by zz order by count(zz) DESC
试试
Ray_Adon 2009-07-17
  • 打赏
  • 举报
回复
:( ,可是还是会提示报错,如果是那样,我也只能用笨点方法在写一条搜索语句了。

select user,username from admin where len(username)>0 and user<>'admin' and user not in (select zz from news)

不过作为一个具备优秀程序员思想的我,写这样的冗余语句出来,心里肯定要不爽一天了。
子陌红尘 2009-07-17
  • 打赏
  • 举报
回复
ACCESS数据库的语法跟SQL SERVER有点区别,但是应该支持表连接吧。
Ray_Adon 2009-07-17
  • 打赏
  • 举报
回复
#4 麦蒂大大报错是:
Microsoft JET Database Engine 错误 '80040e14'

用于函数参数的个数不对 在查询表达式 'isnull(count(zz),0)' 中。
Ray_Adon 2009-07-17
  • 打赏
  • 举报
回复
Sql="select top 18 a.[user],count(n.zz) as num from admin a left join news n on a.[user]=n.zz and n.sh=1 and len(a.username)>0 and a.[user] <>'admin' group by [user] order by count(n.zz) DESC"

#1 libin_ftsafe大大 的语句,提示报错
Microsoft JET Database Engine 错误 '80040e14'

不支持连接表达式。

我的是ASP + ACCESS ,会不会和这个有关系
加载更多回复(2)

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧