我有一个名为archive的表:
| staff_id | longname | username | projectNo | title | process | creation_time |
| 01 | John Doe | John | 1701 | project1 | ANM(ld) | 2017-01-16 |
| 02 | Aaron Jones | Aaron | 1701 | project1 | ANM(ch) | 2017-01-16 |
| 03 | James Bond | James | 1702 | project2 | BGD(ld) | 2017-01-16 |
| 01 | John Doe | John | 1702 | project2 | CGI(ld) | 2017-01-16 |
| 01 | John Doe | John | 1701 | project1 | ANM(ld) | 2017-03-16 |
| 02 | Aaron Jones | Aaron | 1701 | project1 | BGD(ch) | 2017-03-16 |
| 03 | James Bond | James | 1702 | project2 | BGD(ld) | 2017-03-16 |
| 01 | John Doe | John | 1702 | project2 | CGI(ld) | 2017-03-16 |
我想选择最新日期和最早日期,然后比较这两个查询并仅显示已更改的行。所以从上面的表格中可以看出:
| staff_id | longname | username | projectNo | title | process | creation_time |
| 02 | Aaron Jones | Aaron | 1701 | project1 | BGD(ch) | 2017-03-16 |
因为只有这一行被改变了。
我知道如何在查询中选择最早的日期:
SELECT staff_id, longname, username, title, process, creation_time FROM archived
WHERE creation_time IN (SELECT min(creation_time) FROM archived)
最新日期:
SELECT staff_id, longname, username, title, process, creation_time FROM archived
WHERE creation_time IN (SELECT max(creation_time) FROM archived)
问题是如何比较查询并仅显示更改?
好的,我所拥有的是表存档:
| staff_id | longname | username | projectNo | title | process | creation_time |
| 01 | John Doe | John | 1701 | project1 | ANM(ld) | 2017-01-16 |
| 02 | Aaron Jones | Aaron | 1701 | project1 | ANM(ch) | 2017-01-16 |
| 03 | James Bond | James | 1702 | project2 | BGD(ld) | 2017-01-16 |
| 01 | John Doe | John | 1702 | project2 | CGI(ld) | 2017-01-16 |
| 01 | John Doe | John | 1701 | project1 | ANM(ld) | 2017-03-16 |
| 02 | Aaron Jones | Aaron | 1701 | project1 | BGD(ch) | 2017-03-16 |
| 03 | James Bond | James | 1702 | project2 | BGD(ld) | 2017-03-16 |
| 01 | John Doe | John | 1702 | project2 | CGI(ld) | 2017-03-16 |
表格每天00:00:00更新。所以最新的一天会有所不同。我想要的是带第一个日期的行:
| 01 | John Doe | John | 1701 | project1 | ANM(ld) | 2017-01-16 |
| 02 | Aaron Jones | Aaron | 1701 | project1 | ANM(ch) | 2017-01-16 |
| 03 | James Bond | James | 1702 | project2 | BGD(ld) | 2017-01-16 |
| 01 | John Doe | John | 1702 | project2 | CGI(ld) | 2017-01-16 |
最新日期:
| 01 | John Doe | John | 1701 | project1 | ANM(ld) | 2017-03-16 |
| 02 | Aaron Jones | Aaron | 1701 | project1 | BGD(ch) | 2017-03-16 |
| 03 | James Bond | James | 1702 | project2 | BGD(ld) | 2017-03-16 |
| 01 | John Doe | John | 1702 | project2 | CGI(ld) | 2017-03-16 |
然后逐行比较。例如:
| 01 | John Doe | John | 1701 | project1 | ANM(ld) | 2017-01-16 |
与
| 01 | John Doe | John | 1701 | project1 | ANM(ld) | 2017-03-16 |
行是一样的,所以我不需要将它们显示为
| 01 | John Doe | John | 1702 | project2 | CGI(ld) | 2017-01-16 |
与
| 01 | John Doe | John | 1702 | project2 | CGI(ld) | 2017-03-16 |
这些行也一样。所以我想要打印的唯一行是
| staff_id | longname | username | projectNo | title | process | creation_time |
| 02 | Aaron Jones | Aaron | 1701 | project1 | BGD(ch) | 2017-03-16 |
因为它与众不同。
答案 0 :(得分:3)
试试这个:
select tmax.*
from yourtable tmax
inner join (
select staff_id, max(`creation_time`) as maxtime, min(`creation_time`) as mintime
from yourtable
group by staff_id
) t on tmax.staff_id = t.staff_id and tmax.creation_time = t.maxtime
inner join yourtable tmin
on t.staff_id = tmin.staff_id and tmin.creation_time = t.mintime
where tmax.projectNo <> tmin.projectNo or tmax.title <> tmin.title or tmax.`process` <> tmin.`process`
请在此处查看sqlfiddle demo。
子查询将获取每个staff_id的最大时间和最小时间,然后使用staff_id和最大,最小时间两次加入您的表,其中statement是您的标准。