数据库如何在查询出记录的同时,统计记录有多少行

例如:select u.userId from user as u where u.userId<500,能否在查询的同时获取到记录的行数?

select userId,count(userId) from user where userId<500

select u.userId,(select 1 from user as u where u.userId<500) cout from user as u where u.userId<500

用COUNT():

 select count(u.userId) from user as u where u.userId<500

select @@rowcount

@@rowcount不但可以统计查询到多少行,还可统计删除多少行,修改多少行,插入多少行,在使用删除 修改 插入语句的时候
https://msdn.microsoft.com/zh-cn/library/ms187316.aspx

不能这样想的,如果有分页你那个总数就不准确啦

select u.userId,count(1) count from user as u where u.userId<500

count 列代表你查询出来的行数

聚合函数用来做统计分析用,count()也只是聚合函数的一种。
select u.userId,count(1) as count from user as u where u.userId<500;

使用聚合函数 count()