按组获取最新值(用于加入)
Get newest values by groups (for join)
我有两个 table。在 xDATA
我有关于任务的信息,在 xSTATUS
我有任务的进展。
它是这样的:
{xDATA}
ID|description|...
------------------
1|...
2|...
3|...
xSTATUS
table中的数据由时间戳指定:
{xSTATUS}
timestamp |ID|status
--------------------------------
2015-03-22 12:22:33| 1|Added
2015-03-22 14:00:03| 1|Assigned
2015-03-23 08:00:00| 2|Added
2015-03-23 12:00:00| 1|Completed
对于预览,我想要 ID
、description
和最新的 status
。有什么方法可以获取最新值(例如使用 GROUP BY
)?
感谢您的帮助!
您可以将left join
用作
select
xd.*,
st.status
from xdata xd join xstatus st on st.id = xd.id
left join xstatus st1 on st.id = st1.id
and st.timestamp < st1.timestamp
where st1.id is null ;
还有其他可用的技术,请在此处查看 http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html
我有两个 table。在 xDATA
我有关于任务的信息,在 xSTATUS
我有任务的进展。
它是这样的:
{xDATA}
ID|description|...
------------------
1|...
2|...
3|...
xSTATUS
table中的数据由时间戳指定:
{xSTATUS}
timestamp |ID|status
--------------------------------
2015-03-22 12:22:33| 1|Added
2015-03-22 14:00:03| 1|Assigned
2015-03-23 08:00:00| 2|Added
2015-03-23 12:00:00| 1|Completed
对于预览,我想要 ID
、description
和最新的 status
。有什么方法可以获取最新值(例如使用 GROUP BY
)?
感谢您的帮助!
您可以将left join
用作
select
xd.*,
st.status
from xdata xd join xstatus st on st.id = xd.id
left join xstatus st1 on st.id = st1.id
and st.timestamp < st1.timestamp
where st1.id is null ;
还有其他可用的技术,请在此处查看 http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html