MySQL 计算有多少行具有相同的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16707780/
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
Count how many rows have the same value
提问by user2273278
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 |
+-----+-----+
回答by Meherzad
回答by Sirko
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
回答by Dhwani
FOR SPECIFIC NUM:
对于特定号码:
SELECT COUNT(1) FROM YOUR_TABLE WHERE NUM = 1
FOR ALL NUM:
对于所有 NUM:
SELECT NUM, COUNT(1) FROM YOUR_TABLE GROUP BY NUM
回答by Deval Shah
SELECT
COUNT(NUM) as 'result'
FROM
Table1
GROUP BY
NUM
HAVING NUM = 1
回答by Luv
Try this Query
试试这个查询
select NUM, count(1) as count
from tbl
where num = 1
group by NUM
--having count(1) (You condition)
回答by user8567224
SELECT SUM(IF(your_column=3,1,0)) FROM your_table WHERE your_where_contion='something';
SELECT SUM(IF(your_column=3,1,0)) FROM your_table WHERE your_where_contion='something';
e.g. for you query:-
例如为您查询:-
SELECT SUM(IF(num=1,1,0)) FROM your_table_name;
SELECT SUM(IF(num=1,1,0)) FROM your_table_name;
回答by 0xAli
SELECT sum(num) WHERE num = 1;
回答by Rohit Sahu
Use this query this will give your output:
使用此查询将提供您的输出:
select
t.name
,( select
count (*) as num_value
from Table
where num =t.num) cnt
from Table t;