将 count(*) 作为选择查询的一部分是否错误

发布于 2024-11-01 08:41:04 字数 142 浏览 1 评论 0原文

select id, first_name, count(*) from users;

users 表包含 10 个条目,但上面的 select 查询仅显示一行。将 count(*) 混合作为上述查询的一部分是否错误?

select id, first_name, count(*) from users;

The users table contains 10 entries, but the above select query shows only a single row. Is it wrong to mix count(*) as part of the above query?

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

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

发布评论

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

评论(7

最美不过初阳 2024-11-08 08:41:04

COUNT 是一个聚合函数。您不能将其混合到正常查询中。

如果您想接收十个条目,只需执行正常的选择:

SELECT id, name FROM users;

并获取条目数:

SELECT COUNT(id) FROM users;

COUNT is a function that aggregates. You can't mix it into your normal query.

If you want to receive the ten entries just do a normal select:

SELECT id, name FROM users;

and to get the number of entries:

SELECT COUNT(id) FROM users;
灯角 2024-11-08 08:41:04

这是因为您在查询的选择部分使用聚合函数,

要返回 10 条记录,您只需要查询中的 id 和 first_name 。

EG:

SELECT id, first_Name
FROM users

如果您想获取表中记录的计数,那么您可以使用

SELECT (Count(id))
FROM [users]

Its becuase you are using an aggregate function in the select part of the query,

to return the 10 records you just need the id, and first_name in the query.

EG:

SELECT id, first_Name
FROM users

if you wanted to get a count of the records in the table then you could use

SELECT (Count(id))
FROM [users]
猫七 2024-11-08 08:41:04

这不是“错误”,但如果没有“group by”子句,它就毫无意义 - 大多数数据库都会拒绝该查询,因为如果您包含其他列,聚合函数应该包含group by。

It's not "wrong", but it is meaningless without a "group by" clause - most databases will reject that query, as aggregate functions should include a group by if you're including other columns.

猫瑾少女 2024-11-08 08:41:04

不确定您到底想通过此实现什么目的?

select id, first_name,(select count(*) from users) AS usercount from users;

将给出每个单独的用户和总数,但同样,不确定您为什么需要它。

Not sure exactly what you're trying to achieve with this?

select id, first_name,(select count(*) from users) AS usercount from users;

will give each individual user and the total count but again, not sure why you would want it.

白芷 2024-11-08 08:41:04
select id, first_name from users,(select count(*) as total from users) as t;
select id, first_name from users,(select count(*) as total from users) as t;
薆情海 2024-11-08 08:41:04

COUNT 是一个聚合函数,除非与group by结合使用,否则它将始终为您提供表中所有记录的计数。

如果你将它与普通查询结合使用,那么它将优先决定最终输出,因为在你的情况下它返回 1。

如果你想返回所有 10 条记录,你应该写 -

select id,first_name from users

COUNT is an aggregate function and it will always give you count of all records in table unless used in combination with group by.

If you use it in combination with normal query, then it will take priority in deciding the final output as in your case it returns 1.

If you want to return all 10 records, you should just write -

select id,first_name from users
如此安好 2024-11-08 08:41:04

如果需要表中的行数,可以使用 MySQL 的 SQL_CALC_FOUND_ROWS 子句。检查 MySQL 文档以了解它是如何使用的。

If you need number of rows in a table, you can use MySQL's SQL_CALC_FOUND_ROWS clause. Check MySQL docs to see how it's used.

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