2011-07-01 49 views
5

我想選擇表中的所有內容,並且還計算具有相同數據的表中的行數。mySql:計數列中具有相同數據的行數

SELECT *, COUNT(thedate) daycount FROM `table` ORDER BY thedate DESC 

我希望有一個查詢,輸出與該日期相關聯行的日期和數量,以及環形輸出將是這樣的:

2000年1月1日(2行)
COL1,COL2,COL3,COL4
COL1,COL2,COL3,COL4

2000年1月1日(3行)
COL1,COL2,COL3,COL4
COL1,COL2,COL3,COL4
COL1,COL2,COL3,COL4

2000年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

等等

這有意義嗎?

回答

3

如果你有一個表,看起來像這樣:

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> 

我要把它留給你想象力創造力遍歷這個和打印

  • 的DateField
  • datefield_count
  • 打印all_other_fields 'datefield_count' 次

試試看!

0
SELECT ... 
FROM yourtable 
GROUP BY DATE(datefield) 
ORDER BY COUNT(DATE(datefield)) DESC 

請注意,我使用DATE()函數,以防日期字段實際上是日期時間。如果你按照日期時間分組,它會按照完整的yyyy-mm-dd hh:mm:ss分組,而不僅僅是yyyy-mm-dd,你會得到完全不同的結果。

這會讓你獲得核心結果。根據需要執行輸出將需要腳本中的一些後處理,但不是太難。只需緩衝找到的行直到日期發生變化,然後用行數輸出緩衝區。

+0

我正確的認爲這個查詢不會返回*(所有)從表中?只是分組數據? 「我試圖選擇表中的所有內容,並且還要統計表中具有相同數據的行數。」 – superUntitled

+1

你不能雙方都有。分組讓您可以執行聚合函數,但也會「隱藏」每個組的重複成員。如果你想要兩個,你需要做兩個查詢。或者做一個未分組的查詢,然後做聚合函數客戶端。 –

0
SELECT *, COUNT(thedate) daycount 
FROM `table` 
GROUP BY thedate 
ORDER BY thedate DESC 
0
SELECT thedate, COUNT(id) 
FROM table 
WHERE 1 
GROUP BY thedate 
ORDER BY thedate 
1

這不是OP所要求的,而是我在談到這個問題時一直在尋找的東西。也許有些人會覺得它有用。

select * 
    from thetable 
    join (
     select thedate, count(thedate) as cnt 
     from thetable 
     group by thedate 
    ) as counts 
    using(thedate) 
    order by thedate 

上面的查詢將選擇一個額外的字段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

相關問題