计算没有count(*)&的记录第3行的记录 [英] count records without count(*) & record of row num 3

查看:44
本文介绍了计算没有count(*)&的记录第3行的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)我想获取第3行的记录,如何?

2)如何计数记录?count(*)除外.

1)I want to fetch record of row num 3,How ?

2)How can i count the records ?except count(*).

推荐答案

您没有提到这是什么数据库.

1)您可以使用行号"完成各种技巧.此示例适用于SQL 2008

You don''t mention what database this is.

1) There are various tricks you can do with ''Row Number''. This example is for SQL 2008

WITH OrderedOrders AS
(
    SELECT SalesOrderID, OrderDate,
    ROW_NUMBER() OVER (ORDER BY OrderDate) AS 'RowNumber'
    FROM Sales.SalesOrderHeader
)
SELECT *
FROM OrderedOrders
WHERE RowNumber BETWEEN 50 AND 60;



http://msdn.microsoft.com/en-us/library/ms186734.aspx [ ^ ]

因此,您可以为表创建一些SQL并显示WHERE RowNumber = 3


2)为什么您不想使用专门为此目的设计的功能,这超出了我的范围-必须是一项家庭作业!

正如其他答案所建议的那样,您需要对行进行迭代并自己进行计数.

如果您仅需要使用SQL来执行此操作(同样,我假设使用SQL Server),则可以打开游标并遍历各行,递增变量并返回结果.



http://msdn.microsoft.com/en-us/library/ms186734.aspx[^]

So, you could create some SQL for your table and show WHERE RowNumber = 3


2) Why you wouldn''t want to use the function that is specifically designed for this purpose is beyond me - must be a homework task!

As the other answers have suggested, you''ll need to iterate the rows and count them yourself.

If you need to do this purely in SQL (and again, I''m assuming SQL Server) then you could open a cursor and loop through the rows, incrementing a variable and returning the result.


在关键字SELECT之后添加关键字SQL_CALC_FOUND_ROWS:

从t1,t2,t3中选择SQL_CALC_FOUND_ROWS t3.id,其他对象,其他东西(将t1,t2和t3相互关联)GROUP BY t3.id限制10,20

之后,使用函数FOUND_ROWS()运行另一个查询:

SELECT FOUND_ROWS();它应该返回不带LIMIT子句的行数.
Add the keyword SQL_CALC_FOUND_ROWS right after the keyword SELECT :

SELECT SQL_CALC_FOUND_ROWS t3.id, a,bunch,of,other,stuff FROM t1, t2, t3 WHERE (associate t1,t2, and t3 with each other) GROUP BY t3.id LIMIT 10,20

After that, run another query with the function FOUND_ROWS() :

SELECT FOUND_ROWS(); It should return the number of rows without the LIMIT clause.


这篇关于计算没有count(*)&的记录第3行的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆