计算mysql

时间:2016-09-28 14:22:55

标签: mysql

mysql> show columns from MYTABLE;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| x     | int(11)      | NO   | PRI | NULL    |       |
| y     | int(11)      | NO   | PRI | NULL    |       |
| c0    | decimal(3,1) | NO   |     | NULL    |       |
| c1    | decimal(3,1) | NO   |     | NULL    |       |
| c2    | decimal(3,1) | NO   |     | NULL    |       |
| c3    | decimal(3,1) | NO   |     | NULL    |       |
| c4    | decimal(3,1) | NO   |     | NULL    |       |
| c5    | decimal(3,1) | NO   |     | NULL    |       |
| c6    | decimal(3,1) | NO   |     | NULL    |       |
| c7    | decimal(3,1) | NO   |     | NULL    |       |
| c8    | decimal(3,1) | NO   |     | NULL    |       |
| c9    | decimal(3,1) | NO   |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+

我想计算以' c'开头的每一栏。超过某个阈值(例如20.0)的值的数量。

在单个列(例如c0)上,我会做类似的事情:

SELECT COUNT(*) FROM MYTABLE WHERE c0 > 20.0;

但是,如何在单个查询中对c1,c2,c3等进行相同操作?

1 个答案:

答案 0 :(得分:1)

我认为这对你有用:

SELECT SUM(IF(c0 > 20, 1, 0)) AS count_0, SUM(IF(c1 > 20, 1, 0)) AS count_1
FROM table