分类: WINDOWS
2009-09-30 14:01:54
有两个表: 一个是论坛附件记录(Table: Images),令一个是发帖纪录(Table:Posts): 表Images含有两个字段: ImageID UserID 表Posts含有两个字段: PostsID UserID 如何写SQL语句,求发帖最少的用户的所发的附件数? 要求输出 UserID, 帖子数目,该用户发的附件数.
SELECT a.userid,b.postnum,COUNT(a.imageid), FROM images a, (SELECT userid, postnum, ROWNUM rn FROM (SELECT userid, COUNT (postid) postnum FROM posts GROUP BY userid ORDER BY userid, postnum) ORDER BY postnum) b WHERE a.userid = b.userid AND b.rn = 1 group by a.userid,b.postnum