2

我需要构建一个 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    |

或最近修改的与 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

4 回答 4

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

如果您想只返回带有 的那些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

于 2013-02-13T15:46:11.197 回答
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'

请注意,Modified Date 的可能问题不包括时间,并且每天有多个编辑 - 确保此记录也包括时间组件。

于 2013-02-13T15:46:40.167 回答
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 演示

于 2013-02-13T15:48:51.730 回答
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;
于 2013-02-13T16:01:13.920 回答