选择计数(*);

发布于 2024-12-19 21:37:15 字数 452 浏览 0 评论 0原文

我有一个数据库 database1,其中有两个表(Table 1Table2)。

Table1 中有 3 行,Table2 中有 2 行。现在,如果我在 database1 上执行以下 SQL 查询 SELECT COUNT(*);,则输出为 "1"

有谁知道这个“1”表示什么?

两个表的定义如下。

CREATE TABLE Table1
(
ID INT PRIMARY KEY,
NAME NVARCHAR(20)
)

CREATE TABLE Table2
(
ID INT PRIMARY KEY,
NAME NVARCHAR(20)
)

I have a database, database1, with two tables (Table 1, Table2) in it.

There are 3 rows in Table1 and 2 rows in Table2. Now if I execute the following SQL query SELECT COUNT(*); on database1, then the output is "1".

Does anyone has the idea, what this "1" signifies?

The definition of the two tables is as below.

CREATE TABLE Table1
(
ID INT PRIMARY KEY,
NAME NVARCHAR(20)
)

CREATE TABLE Table2
(
ID INT PRIMARY KEY,
NAME NVARCHAR(20)
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

分開簡單 2024-12-26 21:37:15

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

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

现在,如果我们不关心任何表,而只想进行标量计算,我们可能需要执行诸如 SELECT 1 + 1 之类的操作。标准不允许这样做,但它很有用,而且大多数数据库都允许(Oracle 不允许,除非最近更改,至少以前不允许)。

因此,这样的裸 SELECT 被视为好像它们有一个 from 子句,该子句指定一个只有一行且没有列的表(当然不可能,但它确实有效)。因此,SELECT 1 + 1 变为 SELECT 1 + 1 FROM ImaginaryTableWithOneRow,它返回单行和单列,其值为 2

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

在执行 SELECT COUNT(*) 时,您执行了与 SELECT COUNT(*) FROM ImaginaryTableWithOneRow 等效的操作,当然会返回 1。

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]

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.

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).

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.

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.

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

澉约 2024-12-26 21:37:15

类似地,下面也返回一个结果。

SELECT 'test'
WHERE  EXISTS (SELECT *)  

该行为的解释(来自 此 Connect 项目)也适用于你的问题。

在 ANSI SQL 中,不允许使用不带 FROM 子句的 SELECT 语句 -
您需要指定表源。所以语句“SELECT 'test'
WHERE EXISTS(SELECT *)
" 应该给出语法错误。这是正确的
行为。

对于 SQL Server 实现,FROM
子句是可选的,并且一直都是这样工作的。所以你可以做
SELECT 1”或“SELECT @v”等,无需表格。在
其他数据库系统,有一个名为“DUAL”的虚拟表,其中一个
row 用于执行诸如“SELECT 1 FROM”之类的 SELECT 语句
Dual;
”或“SELECT @v FROM Dual;”。现在,来到 EXISTS 子句 -
项目列表与语法或结果无关
查询和 SELECT * 在子查询中有效。将其与
事实上,我们允许 SELECT 不带 FROM,你会得到你想要的行为
看。我们可以修复它,但这样做并没有多大价值
可能会破坏现有的应用程序代码。

Along similar lines the following also returns a result.

SELECT 'test'
WHERE  EXISTS (SELECT *)  

The explanation for that behavior (from this Connect item) also applies to your question.

In ANSI SQL, a SELECT statement without FROM clause is not permitted -
you need to specify a table source. So the statement "SELECT 'test'
WHERE EXISTS(SELECT *)
" should give syntax error. This is the correct
behavior.

With respect to the SQL Server implementation, the FROM
clause is optional and it has always worked this way. So you can do
"SELECT 1" or "SELECT @v" and so on without requiring a table. In
other database systems, there is a dummy table called "DUAL" with one
row
that is used to do such SELECT statements like "SELECT 1 FROM
dual;
" or "SELECT @v FROM dual;". Now, coming to the EXISTS clause -
the project list doesn't matter in terms of the syntax or result of
the query and SELECT * is valid in a sub-query. Couple this with the
fact that we allow SELECT without FROM, you get the behavior that you
see. We could fix it but there is not much value in doing it and it
might break existing application code.

岁月流歌 2024-12-26 21:37:15

这是因为您执行了select count(*)而没有指定表。

count 函数返回指定数据集中的行数。如果您没有指定要从中选择的表,则单个选择将仅返回一行 - 因此 count(*) 将返回 1。(在某些版本的 SQL 中,例如 Oracle,您必须指定一个表或类似的数据库对象;Oracle 包含一个虚拟表(称为 DUAL),当不需要特定表时可以选择该表。)

It's because you have executed select count(*) without specifying a table.

The count function returns the number of rows in the specified dataset. If you don't specify a table to select from, a single select will only ever return a single row - therefore count(*) will return 1. (In some versions of SQL, such as Oracle, you have to specify a table or similar database object; Oracle includes a dummy table (called DUAL) which can be selected from when no specific table is required.)

终难愈 2024-12-26 21:37:15

如果不指定要查询的表,通常不会执行 select count(*)。您的数据库服务器可能根据它正在查询的默认系统表为您提供计数“1”。

尝试使用

select count(*) from Table1

没有表名,这是没有意义的。

you wouldn't normally execute a select count(*) without specifying a table to query against. Your database server is probably giving you a count of "1" based on default system table it is querying.

Try using

select count(*) from Table1

Without a table name it makes no sense.

鲜肉鲜肉永远不皱 2024-12-26 21:37:15

没有表名它总是返回 1 无论它是任何数据库....

without table name it always return 1 whether it any database....

岛歌少女 2024-12-26 21:37:15

由于它被标记为 SQL Server,因此 MSDN 指出。

COUNT 始终返回 int 数据类型值。

还,

COUNT(*) 返回组中的项目数。这包括 NULL
值和重复项。

因此,由于您没有提供用于执行 COUNT 的表,默认(假设)是它返回 1

Since this is tagged SQL server, the MSDN states.

COUNT always returns an int data type value.

Also,

COUNT(*) returns the number of items in a group. This includes NULL
values and duplicates.

Thus, since you didn't provide a table to do a COUNT from, the default (assumption) is that it returns a 1.

眼趣 2024-12-26 21:37:15

COUNT 函数返回结果行数。如果不指定任何表,则默认返回1。即,COUNT(*)、COUNT(1)、COUNT(2)、... 将始终返回 1

COUNT function returns the number of rows as result. If you don't specify any table, it returns 1 by default. ie., COUNT(*), COUNT(1), COUNT(2), ... will return 1 always.

岁月蹉跎了容颜 2024-12-26 21:37:15
Select * 

没有 from 子句是“从宇宙中选择所有”,因为您没有过滤掉任何内容。
就你而言,你问“有多少个宇宙?”
这正是我要教的。第一天我会在黑板上写下:
选择*并询问它的含义。答:给我整个世界。
从那里我会教如何将宇宙过滤成有意义的东西。

我必须承认,我从来没有想过 Select Count(*),这会让它变得更有趣,但仍然会带回一个真实的答案。我们只有一个世界。
如果不咨询史蒂文·霍金,SQL 将不得不与只有 1 的情况进行竞争。

查询的结果是正确的。

Select * 

without a from clause is "Select ALL from the Universe" since you have filtered out nothing.
In your case, you are asking "How many universe?"
This is exactly how I would teach it. I would write on the board on the first day,
Select * and ask what it means. Answer: Give me the world.
And from there I would teach how to filter the universe down to something meaningful.

I must admit, I never thought of Select Count(*), which would make it more interesting but still brings back a true answer. We have only one world.
Without consulting Steven Hawking, SQL will have to contend with only 1.

The results of the query is correct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文