首页 文章资讯内容详情

在MySQL表中计算总数和真实条件值?

2026-06-05 1 花语

为此,您可以使用COUNT()。让我们首先创建一个表-

create table DemoTable ( Value int );

使用插入命令在表中插入一些记录-

insert into DemoTable values(10); insert into DemoTable values(NULL); insert into DemoTable values(20); insert into DemoTable values(40);

使用select语句显示表中的所有记录-

select *from DemoTable;

这将产生以下输出-

+-------+ | Value | +-------+ | 10 | | NULL | | 20 | | 40 | +-------+ 4 rows in set (0.00 sec)

以下是查询以计算MySQL表中的总和真实条件值的查询-

select count(*) as TOTAL_COUNT,count(Value OR NULL) as CONDITION_TRUE from DemoTable;

这将产生以下输出-

+-------------+----------------+ | TOTAL_COUNT | CONDITION_TRUE | +-------------+----------------+ | 4 | 3 | +-------------+----------------+ 1 row in set (0.00 sec)