如何编写 SQL 查询来计算表的 num 列中特定 num 值的总数?
假设我们有以下数据。
姓名 | NUM |
---|---|
山姆 | 1 |
鲍勃 | 1 |
杰克 | 2 |
约翰 | 4 |
进行以下查询:
SELECT WHERE num = 1;
这将返回这两行。
姓名 | NUM |
---|---|
山姆 | 1 |
鲍勃 | 1 |
如果你想得到所有值的结果NUM
:
SELECT `NUM`, COUNT(*) AS `count`
FROM yourTable
GROUP BY `NUM`
或者只是针对一个特定的:
SELECT `NUM`, COUNT(*) AS `count`
FROM yourTable
WHERE `NUM`=1
对于特定号码:
SELECT COUNT(1) FROM YOUR_TABLE WHERE NUM = 1
对于所有号码:
SELECT NUM, COUNT(1) FROM YOUR_TABLE GROUP BY NUM
SELECT
COUNT(NUM) as 'result'
FROM
Table1
GROUP BY
NUM
HAVING NUM = 1
试试这个查询
select NUM, count(1) as count
from tbl
where num = 1
group by NUM
--having count(1) (You condition)
SELECT sum(num) WHERE num = 1;
SELECT SUM(IF(your_column=3,1,0)) FROM your_table WHERE your_where_contion='something';
例如为您查询:-
SELECT SUM(IF(num=1,1,0)) FROM your_table_name;
使用此查询,这将提供您的输出:
select
t.name
,( select
count (*) as num_value
from Table
where num =t.num) cnt
from Table t;