我对 MySQL DBMS 的性能有疑问。也许是一件小事。有两个表,我需要得到如下结果:
PLAYERS VISITS
ID | PLAYER_NAME ID | PLAYER_ID | SEEN
---------------- ---------------------------
1 | user 1 1 | 2 | 2012-12-12
2 | user 2 2 | 2 | 2012-12-13
3 | user 3 3 | 3 | 2012-12-13
4 | user 4 4 | 3 | 2012-12-14
5 | 3 | 2012-12-14
6 | 2 | 2012-12-15
RESULT:
ID | PLAYER_NAME | LAST_SEEN
----------------------------
1 | user 1 | NULL / 'NEVER'
2 | user 2 | 2012-12-15
3 | user 3 | 2012-12-14
4 | user 4 | NULL / 'NEVER'
我目前的查询是:
SELECT
players.id,
players.player_name,
MAX(visits.seen) AS last_seen
FROM players
LEFT JOIN visits ON players.id = visits.player_id
GROUP BY players.id,players.player_name
对我有用,但在我看来,它应该是一种更有效的方法。
它只是更大查询的关键部分。
托马斯