计算总行数,无限制
目前它正在工作,但我希望 xml 包含没有限制的总行数。
SET @query_result = (SELECT ID,Title
FROM
(
SELECT items.id AS "ID",items.title AS "Title" ,
ROW_NUMBER() OVER(ORDER BY date_added DESC) AS RowNum
FROM [cars]
JOIN [items] ON items.id=cars.item_id
WHERE
rejected = 0
)AS MyDerivedTable
WHERE
MyDerivedTable.RowNum BETWEEN (@page-1)*2+1 AND (@page*2)
FOR XML PATH('car'),ROOT('items')
)
这返回
<items>
<car>
<ID>37</ID>
<Title>Used 2004 Chevrolet Corvette Convertible</Title>
</car>
</items>
我想要的
<items>
<car>
<ID>37</ID>
<Title>Used 2004 Chevrolet Corvette Convertible</Title>
<Count>6</Count>
</car>
</items>
虽然计数不是返回的行数,而是与查询匹配的行总数。或者,如果我的问题对任何人来说都难以理解,我正在寻找 FOUND_ROWS() 的 MSSQL 替代方案; 这个问题@SQL Count 总行数使用 LIMIT 试图回答同样的问题,但我想要一个解决方案是 MSSQL。
Currently its working but I want the xml to contain the total number of rows without the limit.
SET @query_result = (SELECT ID,Title
FROM
(
SELECT items.id AS "ID",items.title AS "Title" ,
ROW_NUMBER() OVER(ORDER BY date_added DESC) AS RowNum
FROM [cars]
JOIN [items] ON items.id=cars.item_id
WHERE
rejected = 0
)AS MyDerivedTable
WHERE
MyDerivedTable.RowNum BETWEEN (@page-1)*2+1 AND (@page*2)
FOR XML PATH('car'),ROOT('items')
)
This returns
<items>
<car>
<ID>37</ID>
<Title>Used 2004 Chevrolet Corvette Convertible</Title>
</car>
</items>
I want
<items>
<car>
<ID>37</ID>
<Title>Used 2004 Chevrolet Corvette Convertible</Title>
<Count>6</Count>
</car>
</items>
While Count is not the number of rows returned but the total number of rows that matched the query .Or if my problem is too hard for anybody to understand ,I am looking for MSSQL alternative for FOUND_ROWS();
This question @SQL Count total number of rows whilst using LIMIT is trying to answer the same thing but I want a solution is MSSQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我希望我现在明白你想要做什么。我相信您必须“扭转”您的选择并使用 CTE(通用表表达式)而不是子选择来实现此目的。
试试这个:
应该输出您的
ID
、Title
和Count
(这是RowNum
的最大值)对于每个汽车入口。OK, I hope I understand now what you're trying to do. I believe you have to "turn around" your select and use a CTE (Common Table Expression) instead of a subselect to achieve this.
Try this:
That should output your
ID
,Title
and theCount
(which is the max of theRowNum
) for each car entry.