sql语句 查询记录数

2025-02-27 02:36:35
推荐回答(4个)
回答1:

sql中查询记录数用count函数。

1、创建测试表,插入数据:

create table test
(id int)

insert into test values (1)
insert into test values (2)
insert into test values (3)
insert into test values (null)

2、查询记录数为两种,一种是count(*),一种是count(字段值):

测试一:

select count(*) from test

结果:

测试二:

select count(id) from test

结果:

说明:如果count(字段名)的字段中含有空值,则在count中不计数,而count(*)则是查询全部的行数。

回答2:

top 50是查询前面50个记录,而不是查询数量
select top 50 * from table where 字段 is null

回答3:

DECLARE @n INT
SET @n = 500
SET ROWCOUNT @n
SELECT * FROM Table_name
这样,查询结果将等同于

SELECT TOP 50 FROM Table_name

回答4:

select count(*) from (select top 50 * from table) a where 字段=''