MySQL需要在group by中返回结果集中的特定记录

时间:2013-02-13 15:39:14

标签: mysql select join group-by

我需要构建一个MySQL查询,我可以根据一个或多个列值将结果组合在一起,然后在该组中返回符合某些条件的完整记录。

将下面的表格作为我的数据的代表:

| ID | NAME | Editor | Modified date | Type |
|  1 | doc1 | Smith  | 2012-01-01    | A    |
|  2 | doc2 | Smith  | 2012-03-03    | A    |
|  3 | doc1 | Jones  | 2012-10-10    | A    |
|  4 | doc1 | Scott  | 2011-01-01    | A    |
|  5 | doc2 | Boyd   | 2013-01-01    | A    |
|  6 | doc3 | Smith  | 2010-01-01    | B    |
|  7 | doc4 | Smith  | 2010-03-03    | B    |
|  8 | doc3 | Jones  | 2010-10-10    | B    |
|  9 | doc3 | Scott  | 2009-01-01    | B    |
| 10 | doc4 | Boyd   | 2011-01-01    | B    |

我想返回与任何文件有关的最近修改的记录,即

| ID | NAME | Editor | Modified date | Type |
|  3 | doc1 | Jones  | 2012-10-10    | A    |
|  5 | doc2 | Boyd   | 2013-01-01    | A    |
|  8 | doc3 | Jones  | 2010-10-10    | B    |
| 10 | doc4 | Boyd   | 2011-01-01    | B    |

或者moste最近修改了与A类有关的记录

| ID | NAME | Editor | Modified date | Type |
|  3 | doc1 | Jones  | 2012-10-10    | A    |
|  5 | doc2 | Boyd   | 2013-01-01    | A    |

我尝试过GROUP BY和HAVING,据我所知他们不会返回整个记录。

附录:我可能需要将“最近的”限定符更改为“在给定日期之前”

4 个答案:

答案 0 :(得分:1)

如果要返回最近修改的行,则可以使用:

select *
from yourtable t1
where `Modified date` = (select max(`Modified date`)
                         from yourtable t2
                         where t1.NAME = t2.NAME
                         group by `NAME`);

请参阅SQL Fiddle with Demo

如果您希望仅返回type='A'的那些,那么以下内容将起作用:

select *
from yourtable t1
where `Modified date` = (select max(`Modified date`)
                         from yourtable t2
                         where t1.NAME = t2.NAME
                         group by `NAME`)
  and type = 'A';

请参阅SQL Fiddle with Demo

答案 1 :(得分:1)

使用子查询:

SELECT tbl.* FROM tbl
INNER JOIN (
    SELECT max(Modifed_date) as newestRecordDate, Editor
    FROM tbl
    GROUP BY Editor
) as Newest
on tbl.Modifed_Date = Newest.NewestRecordDate

按类型:

SELECT * FROM tbl
INNER JOIN (
    SELECT max(Modifed_date) as newestRecordDate, Editor
    FROM tbl
    GROUP BY Editor
) as Newest
ON tbl.Modifed_Date = Newest.NewestRecordDate
WHERE Type = 'A'

请注意,修改日期的可能问题不包括时间,每天进行多次编辑 - 确保此记录也包含时间组件。

答案 2 :(得分:0)

试试这个:

第一个问题:

SELECT a.ID, a.name, a.Editor, a.ModifiedDate, a.Type 
FROM tableName a 
INNER JOIN (SELECT b.name, MAX(b.ModifiedDate) ModifiedDate 
            FROM tableName b GROUP BY b.name
           ) b ON a.name = b.name AND a.ModifiedDate = b.ModifiedDate 

<强>输出

| ID | NAME | EDITOR |                   MODIFIEDDATE | TYPE |
--------------------------------------------------------------
|  3 | doc1 |  Jones | October, 10 2012 00:00:00+0000 |    A |
|  5 | doc2 |   Boyd | January, 01 2013 00:00:00+0000 |    A |
|  8 | doc3 |  Jones | October, 10 2010 00:00:00+0000 |    B |
| 10 | doc4 |   Boyd | January, 01 2011 00:00:00+0000 |    B |

Type='A'过滤:

SELECT a.ID, a.name, a.Editor, a.ModifiedDate, a.Type 
FROM tableName a 
INNER JOIN (SELECT b.name, MAX(b.ModifiedDate) ModifiedDate 
            FROM tableName b GROUP BY b.name
           ) b ON a.name = b.name AND a.ModifiedDate = b.ModifiedDate 
WHERE a.Type = 'A'

<强>输出

| ID | NAME | EDITOR |                   MODIFIEDDATE | TYPE |
--------------------------------------------------------------
|  3 | doc1 |  Jones | October, 10 2012 00:00:00+0000 |    A |
|  5 | doc2 |   Boyd | January, 01 2013 00:00:00+0000 |    A |

选中此SQL FIDDLE DEMO

答案 3 :(得分:0)

select * from your_table group by NAME order by Modified_Date DESC;
select * from your_table where Type = 'A' group by NAME order by Modified_Date DESC;