计算有多少行具有相同的值

时间:2022-12-07 20:11:53

How do i write an SQL query to count the total number of a specific num value in the num column of a table.

如何编写SQL查询来计算表的num列中特定的num值的总数。

e.g. select where num = 1

例如,选择num = 1

result: 2

结果:2

+-----+-----+
| NAME | NUM |
+=====+=====+
| SAM  |  1  | 
+-----+-----+
| BOB  |  1  |
+-----+-----+
| JAKE |  2  | 
+-----+-----+  
| JOHN |  4  | 
+-----+-----+

7 个解决方案

#1


82  

Try

试一试

SELECT NAME, count(*) as NUM FROM tbl GROUP BY NAME

SQL FIDDLE

#2


13  

FOR SPECIFIC NUM:

为特定的NUM:

SELECT COUNT(1) FROM YOUR_TABLE WHERE NUM = 1

FOR ALL NUM:

所有矿工工会:

SELECT NUM, COUNT(1) FROM YOUR_TABLE GROUP BY NUM

#3


11  

If you want to have the result for all values of NUM:

如果您想获得NUM的所有值的结果:

SELECT `NUM`, COUNT(*) AS `count` 
FROM yourTable
GROUP BY `NUM`

Or just for one specific:

或者只是一个具体的例子:

SELECT `NUM`, COUNT(*) AS `count` 
FROM yourTable
WHERE `NUM`=1

#4


6  

SELECT 
   COUNT(NUM) as 'result' 
FROM 
   Table1 
GROUP BY 
   NUM 
HAVING NUM = 1

#5


3  

Try this Query

试试这个查询

select NUM, count(1) as count 
from tbl 
where num = 1
group by NUM
--having count(1) (You condition)

SQL FIDDLE

SQL小提琴

#6


1  

SELECT sum(num) WHERE num = 1;

#7


0  

SELECT SUM(IF(your_column=3,1,0)) FROM your_table WHERE your_where_contion='something';

从your_where _where_contion='something'的表中选择SUM(IF(your_column=3,1,0);

e.g. for you query:-

如为您查询:-

SELECT SUM(IF(num=1,1,0)) FROM your_table_name;

选择金额(如果(num = 1 1 0))从your_table_name;

#1


82  

Try

试一试

SELECT NAME, count(*) as NUM FROM tbl GROUP BY NAME

SQL FIDDLE

#2


13  

FOR SPECIFIC NUM:

为特定的NUM:

SELECT COUNT(1) FROM YOUR_TABLE WHERE NUM = 1

FOR ALL NUM:

所有矿工工会:

SELECT NUM, COUNT(1) FROM YOUR_TABLE GROUP BY NUM

#3


11  

If you want to have the result for all values of NUM:

如果您想获得NUM的所有值的结果:

SELECT `NUM`, COUNT(*) AS `count` 
FROM yourTable
GROUP BY `NUM`

Or just for one specific:

或者只是一个具体的例子:

SELECT `NUM`, COUNT(*) AS `count` 
FROM yourTable
WHERE `NUM`=1

#4


6  

SELECT 
   COUNT(NUM) as 'result' 
FROM 
   Table1 
GROUP BY 
   NUM 
HAVING NUM = 1

#5


3  

Try this Query

试试这个查询

select NUM, count(1) as count 
from tbl 
where num = 1
group by NUM
--having count(1) (You condition)

SQL FIDDLE

SQL小提琴

#6


1  

SELECT sum(num) WHERE num = 1;

#7


0  

SELECT SUM(IF(your_column=3,1,0)) FROM your_table WHERE your_where_contion='something';

从your_where _where_contion='something'的表中选择SUM(IF(your_column=3,1,0);

e.g. for you query:-

如为您查询:-

SELECT SUM(IF(num=1,1,0)) FROM your_table_name;

选择金额(如果(num = 1 1 0))从your_table_name;