[英]Selecting top result from SQL
This is i think a simple problem but i can't seem to solve it. 我认为这是一个简单的问题,但我似乎无法解决。
I want to select the newest result from a table and join it with a single element in another table. 我想从一个表中选择最新的结果,并将其与另一个表中的单个元素结合在一起。
To put it better, here's a simple schema: 更好地说,这是一个简单的架构:
Table 1 - Person
personId -PK - INT - AUTO ID
name - VARCHAR
Table 2 - Event
eventId - PK - INT - AUTO ID
personId - FK
timestamp - DATETIME
event - ENUM ('Went Out', 'Came back')
What I'd like to do is return a list of all people and the latest action each person performed 我想做的是返回所有人列表以及每个人执行的最新操作
Example Result: 结果示例:
name|
名称| personId |
personId | timestamp |
时间戳| eventId |
eventId | event
事件
bob |
鲍勃 1 |
1 | 2011-08-7 3 |
2011-08-7 3 | 'went out'
'出去'
I did a simple query joining the two tables and then did a group by personId and order by timestamp but the result that was returned was always the first action for the person, not their latest. 我做了一个简单的查询,将两个表连接在一起,然后按personId和按时间戳顺序进行了分组,但是返回的结果始终是该人的第一个操作,而不是他们的最新操作。
Any Ideas? 有任何想法吗?
SELECT p.name, p.personId, e.timestamp, e.eventId, e.event
FROM person p
INNER JOIN Event e
ON e.eventId =
( SELECT MAX(eventId)
FROM Event
WHERE personId = p.personId
GROUP BY personId
LIMIT 1 )
OR 要么
SELECT p.Name, p.ID, me.timestamp, me.ID, me.event
FROM person p
INNER JOIN (
SELECT id, timestamp, event
FROM Event
WHERE personId = p.ID
ORDER BY timestamp DESC LIMIT 1
) me
ON p.ID = me.id
PS: sorry but can't test both queries right now PS:对不起,但现在无法测试两个查询
SELECT
t1.Name,
t1.PersonId,
t2.TimeStamp,
t2.EventId,
t2.Event
FROM Table1 t1
INNER JOIN Table2 t2 ON t2.PersonId = t1.PersonID
INNER JOIN (SELECT
PersonId,
MAX(TimeStamp) as LastEventDateTime
FROM Table2
GROUP BY PersonID) LE
ON LE.PersonID = t2.PersonID
AND LE.LastEventDateTime = t2.TimeStamp
you'd want to do an 你想做一个
ORDER by `timestamp` DESC
(desc from descending) to get the highest timestamp value instead of the lowest (从降序开始递减)以获取最高的时间戳记值,而不是最低的
The ANSI standard way would be: ANSI标准方式为:
select name, personid, timestamp, eventid, event
from person
join event on event.personid=person.personid
and event.timestamp=(select max(timestamp) from event e2
where e2.personid=person.personid)
I haven't used MySQL in a while and I don't have an installation handy, but you might get what you want with: 我已经有一段时间没有使用MySQL了,也没有安装方便的工具,但是您可能会得到想要的东西:
select name, personid, timestamp, eventid, event
from person
join event on event.personid=person.personid
group by personid
order by personid, timestamp desc
It's non-standard because by the standard, anything in the select must be in the group-by or be an aggregate, and here we don't want to do either. 这是非标准的,因为按照标准,选择中的所有内容都必须在分组依据中或为集合,在这里我们都不希望这样做。 But as I recall MySQL doesn't require that, so I'd give this a whirl and see what happens.
但是,正如我记得MySQL并不需要那样,所以我会旋转一下,看看会发生什么。
An alternative solution, making use of a covered key, assumes that order by Id
would yield the same results as order by timestamp
一种使用覆盖键的替代解决方案,假定
order by Id
将产生与order by timestamp
相同的结果
SELECT p.Name, p.ID, me.timestamp, me.ID, me.event
FROM person p
JOIN (
SELECT personId, MAX(ID) id
FROM Event
WHERE personId = p.ID
GROUP BY personId
) me
ON p.ID = me.id
Order by timestamp
is more natural and probably safer, but this is quicker. Order by timestamp
更自然,也可能更安全,但这更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.