用于计算数字的SQL查询

时间:2014-05-26 09:14:26

标签: mysql sql

我有一个如下所示的列

Video_id
   1
   1
   1
   2
   2
   4
   4
   4
   4

我正在寻找一个可以在SQL中提取计数的查询

Video_id    count
   1          3
   2          2  
   4          4 

4 个答案:

答案 0 :(得分:1)

看起来像COUNT()的简单查询:

SELECT Video_id,COUNT(Video_id) as count
FROM TableName
GROUP BY Video_id

COUNT是一个聚合函数,用于计算列中值的出现次数。
阅读更多here

答案 1 :(得分:1)

试试这个

SELECT Video_id,COUNT(Video_id)
FROM tbl 
Group By Video_id  

<强> FIDDLE DEMO

<强> O / P

VIDEO_ID COUNT(VIDEO_ID)
1        3
2        2
4        4

看一下这些文件

http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html

http://dev.mysql.com/doc/refman/5.1/en/group-by-extensions.html

答案 2 :(得分:0)

使用以下查询:

select Video_id, count(*) as cnt from table_name group by Video_id

答案 3 :(得分:0)

以下是带有样本数据的查询:

with my_table as 
(select 1 videoid from dual
union
select 1 videoid from dual
union
select 1 videoid from dual
union
select 2 videoid from dual
union
select 2 videoid from dual
union
select 4 videoid from dual
union
select 4 videoid from dual
union
select 4 videoid from dual
union
select 4 videoid from dual
)
select videoid,count(1) from my_table
group by videoid;

假设您的表名为“ VIDEO_TABLE ”,那么您的查询将是

SELECT VIDEO_ID, COUNT(1) FROM VIDEO_TABLE
GROUP BY VIDEO_ID
ORDER BY VIDEO_ID;