求:帮我改写下这句SQL语句。。。。

tik_2012 2012-08-17 01:20:17
select top 2 *,number=
(select COUNT(*) from et_ztea_images as i where i.tea_albumid=a.id),
imagepath=(select top 1 imagepath from et_ztea_images as z where
z.tea_albumid=a.id),picid=(select top 1 id from et_ztea_images as z
where z.tea_albumid=a.id) from et_ztea_Album as a where teaid=326
order by AlbumTime desc

我想要让 number为0 数据不要(聚合函数方面)。结果就是把第一条数据去掉。。。。。。。。求救各位大哥~
id userId userName teaid tealevel teaAlbum AlbumTime number imagepath picid
139 NULL NULL 326 1 xx 2012-08-14 0 NULL NULL
47 NULL NULL 326 2 xx 2012-07-27 3 201272778567.jpg 60
...全文
72 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tik_2012 2012-08-17
  • 打赏
  • 举报
回复
问题还是很简单的
Mr_Nice 2012-08-17
  • 打赏
  • 举报
回复
SELECT TOP 2
* ,
number = ( SELECT COUNT(*)
FROM et_ztea_images AS i
WHERE i.tea_albumid = a.id
) ,
imagepath = ( SELECT TOP 1
imagepath
FROM et_ztea_images AS z
WHERE z.tea_albumid = a.id
) ,
picid = ( SELECT TOP 1
id
FROM et_ztea_images AS z
WHERE z.tea_albumid = a.id
)
FROM et_ztea_Album AS a
WHERE teaid = 326
AND EXISTS(SELECT 1 FROM et_ztea_images b WHERE a.id = b.tea_albumid) --加了这句
ORDER BY AlbumTime DESC
叶子 2012-08-17
  • 打赏
  • 举报
回复

SELECT TOP 2
* ,
number = ( SELECT COUNT(*) FROM et_ztea_images AS i WHERE i.tea_albumid = a.id) ,
imagepath = ( SELECT TOP 1 imagepath FROM et_ztea_images AS z WHERE z.tea_albumid = a.id) ,
picid = ( SELECT TOP 1 id FROM et_ztea_images AS z WHERE z.tea_albumid = a.id)
FROM et_ztea_Album AS a
WHERE teaid = 326
AND (SELECT COUNT(*) FROM et_ztea_images AS i WHERE i.tea_albumid = a.id) <> 0
ORDER BY AlbumTime DESC
  • 打赏
  • 举报
回复
改下,with语句中不能含有order by,
换成下面的
select * from
(
select top 2 *,number=
(select COUNT(*) from et_ztea_images as i where i.tea_albumid=a.id),
imagepath=(select top 1 imagepath from et_ztea_images as z where
z.tea_albumid=a.id),picid=(select top 1 id from et_ztea_images as z
where z.tea_albumid=a.id) from et_ztea_Album as a where teaid=326
order by AlbumTime desc
) t
where number<>0
  • 打赏
  • 举报
回复
;with t as
(
select top 2 *,number=
(select COUNT(*) from et_ztea_images as i where i.tea_albumid=a.id),
imagepath=(select top 1 imagepath from et_ztea_images as z where
z.tea_albumid=a.id),picid=(select top 1 id from et_ztea_images as z
where z.tea_albumid=a.id) from et_ztea_Album as a where teaid=326
order by AlbumTime desc
)
select * from t where number<>0

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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