在MySql中计算和分组行

时间:2014-08-27 15:02:09

标签: mysql

我使用Mysql数据库版本5.5.24-log。

在此表中,我无法对字段Premium_name的值进行计数和分组。

在输出中我需要2作为最终值,因为在表中只有两个Premium_name:HYDROSPEED和GLOCKENSPIELMEMORIAL。

提前谢谢。

SELECT
    COUNT(*), Premium_name
FROM
    `table_premium`
GROUP BY
    Premium_name;

+----------+----------------------+
| COUNT(*) | Premium_name         |
+----------+----------------------+
|        2 | GLOCKENSPIELMEMORIAL |
|        6 | HYDROSPEED           |
+----------+----------------------+
2 rows in set


+--------------+------------------+----------------------+--------------+
| Premium_area | Premium_sub_area | Premium_name         | Premium_code |
+--------------+------------------+----------------------+--------------+
| 51PD         | 51PDK1           | HYDROSPEED           |     48069794 |
| 51PD         | 51PDK1           | HYDROSPEED           |     48069795 |
| 51PD         | 51PDK1           | HYDROSPEED           |     49301672 |
| 51PD         | 51PDK1           | HYDROSPEED           |     52480075 |
| 51PD         | 51PDK1           | HYDROSPEED           |     52646404 |
| 51PD         | 51PDK1           | HYDROSPEED           |     91189540 |
| 51PD         | 51PDK2           | GLOCKENSPIELMEMORIAL |     45824206 |
| 51PD         | 51PDK2           | GLOCKENSPIELMEMORIAL |     48479834 |
+--------------+------------------+----------------------+--------------+

3 个答案:

答案 0 :(得分:0)

试试这个:

SELECT COUNT(1) AS PREMIUMS FROM
(SELECT
 DISTINCT Premium_name
 FROM
`table_premium`)CS

答案 1 :(得分:0)

SELECT COUNT(DISTINCT Premium_name), Premium_name 
FROM
    `table_premium`

答案 2 :(得分:0)

SELECT COUNT(DISTINCT Premium_name),Premium_name FROM table_premium group by Premium_name