2011-07-01 10 views
5

テーブル内のすべてを選択しようとしていますが、同じデータを持つテーブル内の行の数もカウントしています。mySql:列内の同じデータを持つ行の数をカウントします。

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

私の希望は、その日付に関連付けられている行の日付と番号を出力する1つのクエリを持つことである、そして、ループの出力は次のようなものになります。

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_count
  • 印刷all_other_fields 'datefield_count' 回

それを試してみます!!!

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

私はDATE()関数を使用していることに注意してください。日付フィールドが実際に日付 - 時間である場合は注意してください。日時にグループ化すると、yyyy-mm-ddだけでなく、完全にyyyy-mm-dd hh:mm:ssでグループ化され、全く異なる結果になります。

それはあなたに中心的な結果をもたらします。必要に応じて出力を行うには、スクリプトに後処理が必要ですが、あまり難しくありません。見つかった行を日付が変わるまでバッファリングし、行数を指定してバッファを出力します。

+0

私はこのクエリがテーブルから*(すべて)を返さないと思っていますか?ちょうどGROUPされたデータ? 「私はテーブル内のすべてを選択しようとしており、同じデータを持つテーブル内の行数を数えます。 – superUntitled

+1

あなたはそれを両方向にすることはできません。グループ化では、関数を集約するだけでなく、各グループの繰り返しメンバーを「隠す」こともできます。両方が必要な場合は、2つのクエリを実行する必要があります。または、グループ化されていないクエリを実行し、集計関数をクライアント側で実行します。 –

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ですべてを選択します。この日
COL1、COL2、COL3、COL4
COL1、COL2、COL3、

COL4いくつかの他の日付、3つのレコードの2つのレコード、

いくつかの日:のようなものを印刷することは簡単ですこの日
COL1、COL2、COL3、COL4
COL1、COL2、COL3、COL4
COL1、COL2、COL3、まだCOL4

他のいくつかの日、この日のために1つのレコードの
col1、col2、col3、col4

関連する問題