为什么没有表名的 MySQL COUNT 给出 1 [英] Why MySQL COUNT without table name gives 1

查看:34
本文介绍了为什么没有表名的 MySQL COUNT 给出 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能解释一下为什么mysql count 函数没有提供任何表名给1 作为值吗?

could you please explain why mysql count function without providing any table name gives 1 as value?

SELECT COUNT(*);

结果: 1

推荐答案

通常所有选择的形式都是 SELECT [列、列上的标量计算、列上的分组计算或标量计算] FROM [表或表的联接,等]

Normally all selects are of the form SELECT [columns, scalar computations on columns, grouped computations on columns, or scalar computations] FROM [table or joins of tables, etc]

因为这允许简单的标量计算,所以我们可以执行诸如 SELECT 1 + 1 FROM SomeTable 之类的操作,它将为 SomeTable 表中的每一行返回一个值为 2 的记录集.

Because this allows plain scalar computations we can do something like SELECT 1 + 1 FROM SomeTable and it will return a recordset with the value 2 for every row in the table SomeTable.

现在,如果我们不关心任何表,而只是想计算我们的标量,我们可能想要做一些类似 SELECT 1 + 1 的事情.这是标准不允许的,但它很有用,而且大多数数据库允许它(Oracle 不允许,除非它最近发生了变化,至少以前没有).

Now, if we didn't care about any table, but just wanted to do our scalar computed we might want to do something like SELECT 1 + 1. This isn't allowed by the standard, but it is useful and most databases allow it (Oracle doesn't unless it's changed recently, at least it used to not).

因此,这种裸 SELECT 被视为有一个 from 子句,该子句指定了一个只有一行没有列的表(当然不可能,但它确实有效).因此 SELECT 1 + 1 变成了 SELECT 1 + 1 FROM ImaginaryTableWithOneRow,它返回单行,单列的值为 2.

Hence such bare SELECTs are treated as if they had a from clause which specified a table with one row and no column (impossible of course, but it does the trick). Hence SELECT 1 + 1 becomes SELECT 1 + 1 FROM ImaginaryTableWithOneRow which returns a single row with a single column with the value 2.

大多数情况下我们不考虑这个,我们只是习惯了裸 SELECT 给出结果的事实,甚至没有考虑必须选择一些单行的东西来返回一行的事实.

Mostly we don't think about this, we just get used to the fact that bare SELECTs give results and don't even think about the fact that there must be some one-row thing selected to return one row.

在执行 SELECT COUNT() 时,您执行了等效于 SELECT COUNT() FROM ImaginaryTableWithOneRow 的操作,当然返回 1.

In doing SELECT COUNT() you did the equivalent of SELECT COUNT() FROM ImaginaryTableWithOneRow which of course returns 1.

参考

这篇关于为什么没有表名的 MySQL COUNT 给出 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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