0

我已经运行了以下 MYSQL 查询:

   SELECT TIMESTAMPDIFF(MONTH,`original_date`, `last_modified_date`) 
   FROM `ft2_form_1` 
   WHERE `success` LIKE '1' 
   AND `color` LIKE '1'

这将返回一系列数字,如下所示: 5 5 5 6 7 7 7 5 6

我想计算返回值,所以结果看起来像这样

    |  value  |   count  |
    ----------------------
    |       5 |        4 |
    |       6 |        2 |
    |       7 |        3 |

我将如何修改我的查询以返回这些结果?

4

2 回答 2

2

尝试这个

SELECT t.value,  COUNT(t.value) FROM
(
       SELECT TIMESTAMPDIFF(MONTH,`original_date`, `last_modified_date`) AS value
       FROM `ft2_form_1` 
       WHERE `success` LIKE '1' 
       AND `color` LIKE '1'
) t
GROUP BY t.value

或者

   SELECT 
      TIMESTAMPDIFF(MONTH,`original_date`, `last_modified_date`) AS value, COUNT(*)
   FROM `ft2_form_1` 
   WHERE `success` LIKE '1' 
   AND `color` LIKE '1'
   GROUP BY TIMESTAMPDIFF(MONTH,`original_date`, `last_modified_date`)
于 2013-04-27T11:44:40.043 回答
0

你可以试试这个查询

SELECT table.value,  COUNT(table.value) FROM
(
    SELECT TIMESTAMPDIFF(MONTH,`original_date`, `last_modified_date`) AS value
    FROM `ft2_form_1`        
        WHERE `success` LIKE '1' 
        AND `color` LIKE '1'
) table
GROUP BY table.value;
于 2014-07-11T06:31:10.040 回答