让我们先说我几乎完成了我的MySQL查询,但我只需要对答案进行最后的暗示。
我使用了以下MySQL查询(供参考):
SELECT * FROM
(
SELECT ll.id AS id, ll.globalId AS globalId, ll.date AS date, ll.serverId AS serverId, ll.gamemodeId AS gamemodeId, ll.mapId AS mapId, origin,
pjl.id AS pjlid, pjl.globalid AS pjlglobalId, pjl.date AS pjldate, MIN(pjl.date) AS mindate, pjl.serverId AS pjlserverId, pjl.playerId AS pjlplayerId
FROM (
(
SELECT id, globalId, date, serverId, playerId, 'playerjoins' AS origin
FROM playerjoins pj
WHERE playerId =976
)
UNION ALL
(
SELECT id, globalId, date, serverId, playerId, 'playerleaves' AS origin
FROM playerleaves pl
WHERE playerId =976
)
ORDER BY date DESC
)pjl
JOIN levelsloaded ll ON pjl.date >= ll.date
GROUP BY ll.id, origin
ORDER BY date DESC) above
这给了我结果集(部分),可以在下面的SQL小提琴中找到:http://sqlfiddle.com/#!2/514b6/1/0
我想要的是以下内容:
id
,请使用id = 133
获取示例结果。id = 113
)。November, 27 2013 00:00:17+0000
。origin = 'playerjoins'
mindate = November, 28 2013 00:00:18+0000
。{/ li>
origin = 'playerleaves'
mindate = November, 28 2013 00:00:19+0000
。{/ li>
playerjoins
是第一次发生的,我希望在我的最终结果集中。所以,我希望我的例子很清楚:我希望每2行具有相同id的行具有最低mindate
的行。我需要能够看到整个行,因此只知道每2行最低mindate
不就足够了。我需要知道origin
。
编辑:答案可能会在https://stackoverflow.com/a/7745635/2057294找到,仍在调查中。
答案 0 :(得分:0)
正确的查询是:
SELECT *
FROM levelsloaded ll
INNER JOIN
(SELECT id, MIN(mindate) AS finalmindate
FROM levelsloaded
GROUP BY id
) ill
ON ll.id = ill.id AND ll.mindate = ill.finalmindate
ORDER BY date DESC
这完全符合我的描述,更详细的答案可以在https://stackoverflow.com/a/7745635/2057294找到。