如何编写 SQL 查询来计算表的 num 列中特定 num 值的总数?
假设我们有以下数据。
请回答以下问题:
SELECT WHERE num = 1;
这将返回这两行。
Try
SELECT NAME, count(*) as NUM FROM tbl GROUP BY NAME
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
FOR SPECIFIC NUM:
SELECT COUNT(1) FROM YOUR_TABLE WHERE NUM = 1
FOR ALL NUM:
SELECT NUM, COUNT(1) FROM YOUR_TABLE GROUP BY NUM
SELECT sum(num) WHERE num = 1;
Try this Query
select NUM, count(1) as count from tbl where num = 1 group by NUM --having count(1) (You condition)
SQL FIDDLE
SELECT COUNT(NUM) as 'result' FROM Table1 GROUP BY NUM HAVING NUM = 1
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;
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;