[英]mySql: count number of rows that have the same data in a column
我正在嘗試 select 表中的所有內容,並計算表中具有相同數據的行數。
SELECT *, COUNT(thedate) daycount FROM `table` ORDER BY thedate DESC
我希望有一個查詢輸出日期和與該日期關聯的行數,循環的 output 將是這樣的:
2000 年 1 月 1 日(2 行)
col1, col2, col3, col4
col1, col2, col3, col42000 年 1 月 1 日(3 行)
col1, col2, col3, col4
col1, col2, col3, col4
col1, col2, col3, col42000 年 1 月 1 日(6 行)
col1, col2, col3, col4
col1, col2, col3, col4
col1, col2, col3, col4
col1, col2, col3, col4
col1, col2, col3, col4
col1, col2, col3, col4
ETC...
這有意義嗎?
如果您有一個如下所示的表:
CREATE TABLE yourtable
(
datefield DATETIME,
col1 VARCHAR(20),
col2 INT NOT NULL,
col3 TINYINT NOT NULL,
col4 CHAR(5)
);
並且您想要每天重復 col1.. col4 的計數,您將運行此查詢
SELECT
COUNT(datefield) datefield_count,
LEFT(all_fields,10) datefield,
SUBSTR(all_fields,11) all_other_fields
FROM
(
SELECT
DATE(datefield) datefield,
CONCAT(DATE(datefield),'|',
COALESCE(col1,'< NULL >'),'|',
COALESCE(col2,'< NULL >'),'|',
COALESCE(col3,'< NULL >'),'|',
COALESCE(col4,'< NULL >'),'|') all_fields
FROM
yourtable
) A
GROUP BY all_fields;
以下是一些示例數據和查詢結果:
mysql> DROP TABLE IF EXISTS yourtable;
Query OK, 0 rows affected (0.04 sec)
mysql> CREATE TABLE yourtable
-> (
-> datefield DATETIME,
-> col1 VARCHAR(20),
-> col2 INT,
-> col3 TINYINT,
-> col4 CHAR(5)
-> );
Query OK, 0 rows affected (0.11 sec)
mysql> INSERT INTO yourtable VALUES
-> (DATE(NOW() - INTERVAL 1 DAY),'rolando',4,3 ,'angel'),
-> (DATE(NOW() - INTERVAL 1 DAY),'rolando',4,3 ,'angel'),
-> (DATE(NOW() - INTERVAL 1 DAY),'rolando',4,3 ,'angel'),
-> (DATE(NOW() - INTERVAL 1 DAY),'rolando',4,NULL,'angel'),
-> (DATE(NOW() - INTERVAL 1 DAY),'rolando',4,NULL,'angel'),
-> (DATE(NOW() - INTERVAL 2 DAY),'rolando',4,2 ,'angel'),
-> (DATE(NOW() - INTERVAL 2 DAY),'rolando',4,2 ,'angel'),
-> (DATE(NOW() - INTERVAL 2 DAY),'rolando',4,2 ,'angel'),
-> (DATE(NOW() - INTERVAL 2 DAY),'rolando',4,2 ,'angel'),
-> (DATE(NOW() - INTERVAL 2 DAY),'rolando',4,NULL,'edwards'),
-> (DATE(NOW() - INTERVAL 2 DAY),'rolando',4,NULL,'angel'),
-> (DATE(NOW() - INTERVAL 3 DAY),'rolando',5,2 ,'angel'),
-> (DATE(NOW() - INTERVAL 3 DAY),'rolando',5,2 ,'angel'),
-> (DATE(NOW() - INTERVAL 3 DAY),'rolando',4,2 ,'angel'),
-> (DATE(NOW() - INTERVAL 3 DAY),'pamela' ,4,2 ,'angel'),
-> (DATE(NOW() - INTERVAL 3 DAY),'pamela' ,4,NULL,'edwards'),
-> (DATE(NOW() - INTERVAL 3 DAY),'pamela' ,5,2 ,'angel'),
-> (DATE(NOW() - INTERVAL 3 DAY),'pamela' ,5,2 ,'angel'),
-> (DATE(NOW() - INTERVAL 3 DAY),'rolando',4,2 ,'angel'),
-> (DATE(NOW() - INTERVAL 3 DAY),'rolando',4,2 ,'angel'),
-> (DATE(NOW() - INTERVAL 3 DAY),'rolando',4,NULL,'edwards'),
-> (DATE(NOW() - INTERVAL 3 DAY),'rolando',4,NULL,'angel')
-> ;
Query OK, 22 rows affected, 3 warnings (0.03 sec)
Records: 22 Duplicates: 0 Warnings: 3
mysql> SELECT * FROM yourtable;
+---------------------+---------+------+------+-------+
| datefield | col1 | col2 | col3 | col4 |
+---------------------+---------+------+------+-------+
| 2011-06-30 00:00:00 | rolando | 4 | 3 | angel |
| 2011-06-30 00:00:00 | rolando | 4 | 3 | angel |
| 2011-06-30 00:00:00 | rolando | 4 | 3 | angel |
| 2011-06-30 00:00:00 | rolando | 4 | NULL | angel |
| 2011-06-30 00:00:00 | rolando | 4 | NULL | angel |
| 2011-06-29 00:00:00 | rolando | 4 | 2 | angel |
| 2011-06-29 00:00:00 | rolando | 4 | 2 | angel |
| 2011-06-29 00:00:00 | rolando | 4 | 2 | angel |
| 2011-06-29 00:00:00 | rolando | 4 | 2 | angel |
| 2011-06-29 00:00:00 | rolando | 4 | NULL | edwar |
| 2011-06-29 00:00:00 | rolando | 4 | NULL | angel |
| 2011-06-28 00:00:00 | rolando | 5 | 2 | angel |
| 2011-06-28 00:00:00 | rolando | 5 | 2 | angel |
| 2011-06-28 00:00:00 | rolando | 4 | 2 | angel |
| 2011-06-28 00:00:00 | pamela | 4 | 2 | angel |
| 2011-06-28 00:00:00 | pamela | 4 | NULL | edwar |
| 2011-06-28 00:00:00 | pamela | 5 | 2 | angel |
| 2011-06-28 00:00:00 | pamela | 5 | 2 | angel |
| 2011-06-28 00:00:00 | rolando | 4 | 2 | angel |
| 2011-06-28 00:00:00 | rolando | 4 | 2 | angel |
| 2011-06-28 00:00:00 | rolando | 4 | NULL | edwar |
| 2011-06-28 00:00:00 | rolando | 4 | NULL | angel |
+---------------------+---------+------+------+-------+
22 rows in set (0.00 sec)
mysql> SELECT
-> COUNT(datefield) datefield_count,
-> LEFT(all_fields,10) datefield,
-> SUBSTR(all_fields,11) all_other_fields
-> FROM
-> (
-> SELECT
-> DATE(datefield) datefield,
-> CONCAT(DATE(datefield),'|',
-> COALESCE(col1,'< NULL >'),'|',
-> COALESCE(col2,'< NULL >'),'|',
-> COALESCE(col3,'< NULL >'),'|',
-> COALESCE(col4,'< NULL >'),'|') all_fields
-> FROM
-> yourtable
-> ) A
-> GROUP BY all_fields;
+-----------------+------------+----------------------------+
| datefield_count | datefield | all_other_fields |
+-----------------+------------+----------------------------+
| 1 | 2011-06-28 | |pamela|4|2|angel| |
| 1 | 2011-06-28 | |pamela|4|< NULL >|edwar| |
| 2 | 2011-06-28 | |pamela|5|2|angel| |
| 3 | 2011-06-28 | |rolando|4|2|angel| |
| 1 | 2011-06-28 | |rolando|4|< NULL >|angel| |
| 1 | 2011-06-28 | |rolando|4|< NULL >|edwar| |
| 2 | 2011-06-28 | |rolando|5|2|angel| |
| 4 | 2011-06-29 | |rolando|4|2|angel| |
| 1 | 2011-06-29 | |rolando|4|< NULL >|angel| |
| 1 | 2011-06-29 | |rolando|4|< NULL >|edwar| |
| 3 | 2011-06-30 | |rolando|4|3|angel| |
| 2 | 2011-06-30 | |rolando|4|< NULL >|angel| |
+-----------------+------------+----------------------------+
12 rows in set (0.00 sec)
mysql>
我會把它留給你富有想象力的創造力來循環並打印
試試看 !!!
這不是OP所要求的,而是我遇到這個問題時一直在尋找的。 也許有些人會發現它很有用。
select *
from thetable
join (
select thedate, count( thedate ) as cnt
from thetable
group by thedate
) as counts
using( thedate )
order by thedate
上面的查詢將 select 帶有一個額外字段cnt的所有內容,其中包含具有相同日期的記錄數。 然后打印如下內容是微不足道的:
某個日期,該日期的 2 條記錄
col1, col2, col3, col4
col1, col2, col3, col4
其他日期,該日期的 3 條記錄
col1, col2, col3, col4
col1, col2, col3, col4
col1, col2, col3, col4
還有其他日期,該日期的 1 條記錄
col1, col2, col3, col4
SELECT ...
FROM yourtable
GROUP BY DATE(datefield)
ORDER BY COUNT(DATE(datefield)) DESC
請注意,我使用的是 DATE() function,以防您的日期字段實際上是日期時間。 如果您按日期時間分組,它將完全按 yyyy-mm-dd hh:mm:ss 分組,而不僅僅是 yyyy-mm-dd,您會得到完全不同的結果。
這將為您提供核心結果。 根據需要執行 output 需要在腳本中進行一些后處理,但並不難。 只需緩沖找到的行,直到日期更改,然后 output 緩沖行數。
SELECT *, COUNT(thedate) daycount
FROM `table`
GROUP BY thedate
ORDER BY thedate DESC
SELECT thedate, COUNT(id)
FROM table
WHERE 1
GROUP BY thedate
ORDER BY thedate
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.