简体   繁体   English

mySql:统计一列有相同数据的行数

[英]mySql: count number of rows that have the same data in a column

I am trying to select everything in a table, and also count the number of rows in a table that have the same data.我正在尝试 select 表中的所有内容,并计算表中具有相同数据的行数。

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

My hope is to have one query that outputs the date and number of rows associated with that date, and the looped output will be something like this:我希望有一个查询输出日期和与该日期关联的行数,循环的 output 将是这样的:

Jan 1, 2000 (2 rows) 2000 年 1 月 1 日(2 行)
col1, col2, col3, col4 col1, col2, col3, col4
col1, col2, col3, col4 col1, col2, col3, col4

Jan 1, 2000 (3 rows) 2000 年 1 月 1 日(3 行)
col1, col2, col3, col4 col1, col2, col3, col4
col1, col2, col3, col4 col1, col2, col3, col4
col1, col2, col3, col4 col1, col2, col3, col4

Jan 1, 2000 (6 rows) 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
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... ETC...

Does this make sense?这有意义吗?

If you have a table that looks like this:如果您有一个如下所示的表:

CREATE TABLE yourtable
(
    datefield DATETIME,
    col1 VARCHAR(20),
    col2 INT NOT NULL,
    col3 TINYINT NOT NULL,
    col4 CHAR(5)
);

and you wanted the counts of duplicate col1.. col4 per given day, you would run this query并且您想要每天重复 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;

Here is some sample data and the result of the query:以下是一些示例数据和查询结果:

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>

I'll leave it to you imaginative creativity to loop through this and print我会把它留给你富有想象力的创造力来循环并打印

  • datefield日期字段
  • datefield_count datefield_count
  • print all_other_fields 'datefield_count' times打印 all_other_fields 'datefield_count' 次

Give it a Try !!!试试看 !!!

This is not what the OP have asked, rather what I have been looking for when I came upon this question.这不是OP所要求的,而是我遇到这个问题时一直在寻找的。 Perhaps some will find it useful.也许有些人会发现它很有用。

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

The above query will select everything with an extra field cnt containing the number of records having the same date.上面的查询将 select 带有一个额外字段cnt的所有内容,其中包含具有相同日期的记录数。 Then it is trivial to print something like:然后打印如下内容是微不足道的:

some date, 2 records for this date某个日期,该日期的 2 条记录
col1, col2, col3, col4 col1, col2, col3, col4
col1, col2, col3, col4 col1, col2, col3, col4

some other date, 3 records for this date其他日期,该日期的 3 条记录
col1, col2, col3, col4 col1, col2, col3, col4
col1, col2, col3, col4 col1, col2, col3, col4
col1, col2, col3, col4 col1, col2, col3, col4

yet some other date, 1 record for this date还有其他日期,该日期的 1 条记录
col1, col2, col3, col4 col1, col2, col3, col4

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

note that I'm using the DATE() function, in case your date fields are actually date-time.请注意,我使用的是 DATE() function,以防您的日期字段实际上是日期时间。 If you group on date time, it'd group by the fully yyyy-mm-dd hh:mm:ss, not just the yyyy-mm-dd and you'd get totally different results.如果您按日期时间分组,它将完全按 yyyy-mm-dd hh:mm:ss 分组,而不仅仅是 yyyy-mm-dd,您会得到完全不同的结果。

That'll get you the core results.这将为您提供核心结果。 Doing the output as you want it will need some post-processing in your script, but isn't too hard.根据需要执行 output 需要在脚本中进行一些后处理,但并不难。 Just buffer the found rows until the date changes, then output the buffer with a row count.只需缓冲找到的行,直到日期更改,然后 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM