0

次の出力があります。

    root@localhost [~]# mysql -e "SELECT TABLE_ROWS from information_schema.Tables where TABLE_SCHEMA= 'testdb' && TABLE_NAME = 'requests';"
    +------------+
    | TABLE_ROWS |
    +------------+
    |    9566846 |
    +------------+

    root@localhost [~]# mysql -e "select count(*) from testdb.requests where created_at like '2012%';"
    +----------+
    | count(*) |
    +----------+
    |   301438 |
    +----------+
    root@localhost [~]# mysql -e "select count(*) from testdb.requests where created_at like '2013%';"
    +----------+
    | count(*) |
    +----------+
    |    24917 |
    +----------+

mysqlリクエストを使用して、1つのリクエストで同じことを行い、次のような新しい出力を得る方法はより良いでしょう

    +------------------+-----------------------+
    | year             | count(*)              |
    +------------------+-----------------------+
    | 2009             | 1066268               |
    | 2010             | 6799553               |
    | 2011             | 1374685               |
    | 2012             | 301438                |
    | 2013             | 24917                 |
    | total            | 9566846               |
    +------------------+-----------------------+

前もってありがとう、エフゲニー

4

3 に答える 3

1

group ステートメントを使用してみることができます

select 
    DATE_FORMAT(created_at, '%d') as year, 
    count(*) as count
from
    testdb.requests
group by year;
于 2013-02-12T07:27:18.647 に答える
1

集計関数を使用してみてください -

SELECT
  YEAR(created_at) yesr,
  COUNT(*) cnt
FROM
  testdb.requests
GROUP BY year;

合計をカウントするには WITH ROLLUP 修飾子を使用します -

SELECT
  YEAR(created_at) yesr,
  COUNT(*) cnt
FROM
  testdb.requests
GROUP BY year WITH ROLLUP;
于 2013-02-12T07:32:02.040 に答える
1

これを試してみてください

SELECT YEAR(created_at) AS `year`,
       COUNT(*) AS `count` 
  FROM testdb.requests 
 GROUP BY YEAR(created_at)
 UNION ALL
SELECT 'total' AS `year`, 
       TABLE_ROWS AS `count`
  FROM information_schema.Tables 
 WHERE TABLE_SCHEMA= 'testdb' AND 
       TABLE_NAME = 'requests'

また

SELECT YEAR(created_at) AS `year`,
       COUNT(*) AS `count` 
  FROM testdb.requests 
 GROUP BY YEAR(created_at)
 UNION ALL
SELECT 'total' AS `year`, 
       COUNT(*) AS `year`
  FROM testdb.requests 

次のような出力が生成されます。

+-------+-------+
| year  | count |
+-------+-------+
| 2012  |     6 |
| 2013  |     7 |
| total |    13 |
+-------+-------+

ここにsqlfiddleがあります

于 2013-02-12T07:34:08.660 に答える