SQL 子查询 - 缺乏经验!

发布于 2024-11-19 02:01:09 字数 95 浏览 1 评论 0原文

我有一个数据库,我试图显示“源”字段中每个不同条目的用户数量。例如,显示有多少人共享相同的来源。我必须通过子查询来做到这一点吗?或者我是否必须真正知道数据库中来源的标题是什么?

I have a database, and I'm trying to show the count of users for each different entry in the "source" field. So for example, show how many people share the same source. Would I have to do this through a subquery? Or do I have to actually know what the titles of the sources are in the database?

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

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

发布评论

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

评论(5

还不是爱你 2024-11-26 02:01:09

您可以使用GROUP BY

SELECT COUNT(*), source FROM mytable GROUP BY source;

You can use GROUP BY:

SELECT COUNT(*), source FROM mytable GROUP BY source;
你与清晨阳光 2024-11-26 02:01:09

听起来你会使用分组:

select source, count(*) from users group by source ;

Sounds like you'd use grouping:

select source, count(*) from users group by source ;
淡看悲欢离合 2024-11-26 02:01:09
SELECT Source, count(Source) as TotalPeople
FROM SomeTable
GROUP BY Source
SELECT Source, count(Source) as TotalPeople
FROM SomeTable
GROUP BY Source
花伊自在美 2024-11-26 02:01:09

您正在寻找GROUP BY查询。

从这里开始: http://www.sql-tutorial.com/sql -group-by-sql-tutorial/

You are looking for GROUP BY query.

Start here: http://www.sql-tutorial.com/sql-group-by-sql-tutorial/

玩世 2024-11-26 02:01:09

这会计算按 myField 的值分组的记录:

SELECT COUNT(1) As rCount FROM myTable GROUP BY myField

您还可以使用多个 GROUP 子句对查询进行分组。
这将为您提供任何子分组的计数:

SELECT myField1, myField2, COUNT(1) As rCount FROM myTable GROUP BY myField1, myField2

对于 Sql 引擎,COUNT(1) 优于 COUNT(*)

This counts the record grouped by the values of myField:

SELECT COUNT(1) As rCount FROM myTable GROUP BY myField

You can also group your query with multiple GROUP clauses.
This will give you the counting of any sub-grouping:

SELECT myField1, myField2, COUNT(1) As rCount FROM myTable GROUP BY myField1, myField2

The COUNT(1) is better than COUNT(*), for the Sql engine.

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