带有 COUNT 帮助的 SQL 子查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3884733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL subquery with COUNT help
提问by thefonso
I have an SQL statement that works
我有一个有效的 SQL 语句
SELECT * FROM eventsTable WHERE columnName='Business'
I want to add this as a subquery...
我想将其添加为子查询...
COUNT(Business) AS row_count
How do I do this?
我该怎么做呢?
回答by codingbadger
This is probably the easiest way, not the prettiest though:
这可能是最简单的方法,但不是最漂亮的:
SELECT *,
(SELECT Count(*) FROM eventsTable WHERE columnName = 'Business') as RowCount
FROM eventsTable
WHERE columnName = 'Business'
This will also work without having to use a group by
这也可以工作而无需使用 group by
SELECT *, COUNT(*) OVER () as RowCount
FROM eventsTables
WHERE columnName = 'Business'
回答by rugg
SELECT e.*,
cnt.colCount
FROM eventsTable e
INNER JOIN (
select columnName,count(columnName) as colCount
from eventsTable e2
group by columnName
) as cnt on cnt.columnName = e.columnName
WHERE e.columnName='Business'
-- Added space
-- 增加了空间
回答by eumiro
Do you want to get the number of rows?
你想得到行数吗?
SELECT columnName, COUNT(*) AS row_count
FROM eventsTable
WHERE columnName = 'Business'
GROUP BY columnName
回答by Noel Abrahams
Assuming there is a column named business:
假设有一个名为 business 的列:
SELECT Business, COUNT(*) FROM eventsTable GROUP BY Business
SELECT Business, COUNT(*) FROM eventsTable GROUP BY Business