count 用法

1

count:通过分组,进行统计,与group by 联合使用,当count()得到的数作为查询条件,需要与having联合使用,而不是where

use datebase_name

go 

create table text1

([number] [int] NOT NULL  primary key,

[indate] [datetime] NULL,

[name] [nvarchar](255) NULL,

[grade] [nvarchar](255) NULL,

[teacher] [nvarchar](255) NULL,

[money] [money] NULL,

[class] [nvarchar](50) NULL,

[course]  [nvarchar](50) NULL)

go 

insert into text1

values(1,'2020-10-27','张三','98','刘四','4000','一年级','英语'),
(2,'2020-10-27','张三','98','刘四','4000','一年级','语文'),
(3,'2020-10-27','张三','98','刘四','4000','一年级','数学'),
(4,'2020-10-27','李四','98','刘四','4000','一年级','英语'),
(5,'2020-10-27','李四','98','刘四','4000','一年级','语文'),
(6,'2020-10-27','李四','98','刘四','4000','一年级','数学');

select count(name),course
from text1
where name = '张三'
group by course
having count(name) is not null

2

count:count可单独使用,统计的是符合条件的所有记录数量,count(0)与count(字段名1 /字段名2)作用相同

select count(0)

from text1

posted @ 2020-10-27 09:39  咸鱼不二  阅读(877)  评论(0编辑  收藏  举报