这应该很简单我只是没有得到理想的结果,我尝试了几种方式,仍然没有得到任何我需要得到子查询记录的总数我知道这是完全错误的但是当时但是当我把计数放在子查询上时,它仍然给我1这里是我的代码:
SELECT COUNT(*) [COUNT]
(
SELECT WP_BlogEntries.BlogEntryID, sys_Objects_Tags.TagID
FROM WP_BlogEntries INNER JOIN sys_Objects_Tags ON
WP_BlogEntries.BlogEntryID = sys_Objects_Tags.SystemObjectRecordID
WHERE WP_BlogEntries.ReleaseDate < GETDATE()
AND WP_BlogEntries.ExpireDate > GETDATE()
AND WP_BlogEntries.Approved = 1
AND WP_BlogEntries.Listed = 1
AND WP_BlogEntries.BlogID = @BlogID
AND TagID = @TagID
GROUP BY WP_BlogEntries.BlogID, BlogEntryID, sys_Objects_Tags.TagID
)
答案 0 :(得分:2)
SELECT COUNT(*) As [COUNT]
From (
SELECT WP_BlogEntries.BlogEntryID, sys_Objects_Tags.TagID
FROM WP_BlogEntries
INNER JOIN sys_Objects_Tags
ON WP_BlogEntries.BlogEntryID = sys_Objects_Tags.SystemObjectRecordID
WHERE WP_BlogEntries.ReleaseDate < GETDATE()
AND WP_BlogEntries.ExpireDate > GETDATE()
AND WP_BlogEntries.Approved = 1
AND WP_BlogEntries.Listed = 1
AND WP_BlogEntries.BlogID = @BlogID
AND TagID = @TagID
GROUP BY WP_BlogEntries.BlogID, BlogEntryID, sys_Objects_Tags.TagID
) As Z
您只需要对子查询进行别名并将其放入FROM子句中,使其成为派生表。虽然,如果你想要的只是计数,你在Select子句中不需要任何东西(也就是说,你可以在子查询的Select子句中使用Select 1
,它仍然可以工作。
答案 1 :(得分:0)
这与@BlogID,@ TagID中的常规参数一起返回多少行:
SELECT WP_BlogEntries.BlogEntryID, sys_Objects_Tags.TagID
FROM WP_BlogEntries INNER JOIN sys_Objects_Tags ON
WP_BlogEntries.BlogEntryID = sys_Objects_Tags.SystemObjectRecordID
WHERE WP_BlogEntries.ReleaseDate < GETDATE()
AND WP_BlogEntries.ExpireDate > GETDATE()
AND WP_BlogEntries.Approved = 1
AND WP_BlogEntries.Listed = 1
AND WP_BlogEntries.BlogID = @BlogID
AND TagID = @TagID
GROUP BY WP_BlogEntries.BlogID, BlogEntryID, sys_Objects_Tags.TagID
而且,正如其他人所指出的那样,你的内部查询需要一个名字:
SELECT COUNT(*) FROM (
SELECT WP_BlogEntries.BlogEntryID, sys_Objects_Tags.TagID
FROM WP_BlogEntries INNER JOIN sys_Objects_Tags ON
WP_BlogEntries.BlogEntryID = sys_Objects_Tags.SystemObjectRecordID
WHERE WP_BlogEntries.ReleaseDate < GETDATE()
AND WP_BlogEntries.ExpireDate > GETDATE()
AND WP_BlogEntries.Approved = 1
AND WP_BlogEntries.Listed = 1
AND WP_BlogEntries.BlogID = @BlogID
AND TagID = @TagID
GROUP BY WP_BlogEntries.BlogID, BlogEntryID, sys_Objects_Tags.TagID
) PLEASE
答案 2 :(得分:0)
select count(*)as COUNT from (select * from tableName where condition='this' group by ID)as s