如何计算总计数的表的个别记录

时间:2014-05-23 05:41:09

标签: php mysql

我需要根据类型计算表中的单个记录。 我和#34;类型"是:

delhi
mumbai
banglore
calcutta 

Delhi consist 8000 records
banglore consist 2000 records
mumbai consist of 31000 record
calcutta consist of 4000 record

我需要这些具有总回忆数的个人记录

Delhi consist 8000 records
banglore consist 2000 records
mumbai consist of 31000 record
calcutta consist of 4000 record
totoal records  45000

我使用查询 -

SELECT TYPE ,COUNT(*) AS COUNT FROM `caselaw` GROUP BY TYPE  ORDER BY COUNT DESC;

我只是得到这个

Delhi consist 8000 records
banglore consist 2000 records
mumbai consist of 31000 record
calcutta consist of 4000 record

但我也需要这个

totoal records  45000

我的表格结构是

id type data 
1  Delhi abc
2 mumbai xyz
3 mumbai mno
1  Delhi xyz
2 mumbai abc
3 mumbai bla
1  Delhi bla
2 banglore  etc
3 mumbai  etc

3 个答案:

答案 0 :(得分:4)

我建议使用UNION查询,如下所示:

SELECT
  TYPE,
  COUNT(*) AS COUNT
FROM `caselaw`
GROUP BY TYPE
ORDER BY COUNT DESC
UNION
SELECT
  'total records' AS TYPE,
  COUNT(*) AS COUNT
FROM `caselaw`;

答案 1 :(得分:0)

更简单的方法

SELECT TYPE ,COUNT(*) TYPE_COUNT, (select count(*) from caselaw) TOTAL_RECORDS
          FROM caselaw GROUP BY TYPE  ORDER BY TYPE_COUNT DESC;

答案 2 :(得分:0)

SELECT TYPE,COUNT()AS COUNT FROM table_name GROUP BY TYPE 联盟 SELECT'总记录' AS TYPE,COUNT()AS COUNT FROM table_name;