“从table_name中选择count(1)"是什么意思?在任何数据库表上是什么意思? [英] What does "select count(1) from table_name" on any database tables mean?

查看:89
本文介绍了“从table_name中选择count(1)"是什么意思?在任何数据库表上是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们执行select count(*) from table_name时,它返回行数.

When we execute select count(*) from table_name it returns the number of rows.

count(1)的作用是什么? 1在这里表示什么?这与count(*)是否相同(因为它在执行时给出相同的结果)?

What does count(1) do? What does 1 signify here? Is this the same as count(*) (as it gives the same result on execution)?

推荐答案

COUNT函数的参数是一个表达式,该表达式将针对每一行进行评估. COUNT函数返回表达式计算结果为非空值的行数. (*是一个不计算的特殊表达式,它只返回行数.)

The parameter to the COUNT function is an expression that is to be evaluated for each row. The COUNT function returns the number of rows for which the expression evaluates to a non-null value. ( * is a special expression that is not evaluated, it simply returns the number of rows.)

该表达式还有两个附加修饰符:ALL和DISTINCT.这些确定重复项是否被丢弃.由于ALL是默认设置,因此您的示例与count(ALL 1)相同,这意味着将保留重复项.

There are two additional modifiers for the expression: ALL and DISTINCT. These determine whether duplicates are discarded. Since ALL is the default, your example is the same as count(ALL 1), which means that duplicates are retained.

由于表达式"1"的每一行求值为非null,并且由于您没有删除重复项,因此COUNT(1)始终应返回与COUNT(*)相同的数字.

Since the expression "1" evaluates to non-null for every row, and since you are not removing duplicates, COUNT(1) should always return the same number as COUNT(*).

这篇关于“从table_name中选择count(1)"是什么意思?在任何数据库表上是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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