从三个简单查询创建复杂查询

时间:2013-11-26 18:50:24

标签: mysql sql

小背景:我一直在大部分时间都在研究这个问题,我很兴奋我几乎找到了解决方案,但我似乎无法将最终的MySQL查询放在一起。这完全是关于MySQL语法的,所以我认为不需要给出表模式等。

查询部分:

1)

SELECT id, globalId, date, serverId, gamemodeId, mapId FROM levelsloaded

2)

(
(SELECT id, globalId, date, serverId, playerId, 'playerjoins' AS origin 
FROM playerjoins WHERE playerId = 2224 AND date <= levelsloaded.date 
ORDER BY date DESC)
UNION ALL
(SELECT id, globalId, date, serverId, playerId, 'playerleaves' AS origin 
FROM playerleaves WHERE playerId = 2224 AND date <= levelsloaded.date 
ORDER BY date DESC)
ORDER BY date DESC LIMIT 1) below

3)

(
(SELECT id, globalId, date, serverId, playerId, 'playerjoins' AS origin 
FROM playerjoins WHERE playerId = 2224 AND date >= levelsloaded.date 
ORDER BY date ASC)
UNION ALL
(SELECT id, globalId, date, serverId, playerId, 'playerleaves' AS origin 
FROM playerleaves WHERE playerId = 2224 AND date >= levelsloaded.date 
ORDER BY date ASC)
ORDER BY date ASC LIMIT 1) above

如果需要,以下是模式:

CREATE TABLE `levelsloaded` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `globalId` int(11) NOT NULL,
  `date` datetime NOT NULL,
  `serverId` int(11) NOT NULL,
  `gamemodeId` int(11) NOT NULL,
  `mapId` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `globalId` (`globalId`),
  KEY `date` (`date`),
  KEY `serverId` (`serverId`),
  KEY `gamemodeId` (`gamemodeId`),
  KEY `mapId` (`mapId`),
  CONSTRAINT `levelsloaded_ibfk_1` FOREIGN KEY (`serverId`) REFERENCES `servers` (`serverId`),
  CONSTRAINT `levelsloaded_ibfk_2` FOREIGN KEY (`gamemodeId`) REFERENCES `gamemodes` (`gamemodeId`),
  CONSTRAINT `levelsloaded_ibfk_3` FOREIGN KEY (`mapId`) REFERENCES `maps` (`mapId`)
) ENGINE=InnoDB AUTO_INCREMENT=1104 DEFAULT CHARSET=latin1

CREATE TABLE `playerjoins` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `globalId` int(11) NOT NULL,
  `date` datetime NOT NULL,
  `serverId` int(11) NOT NULL,
  `playerId` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `globalId` (`globalId`),
  KEY `date` (`date`),
  KEY `serverId` (`serverId`),
  KEY `playerId` (`playerId`),
  CONSTRAINT `playerjoins_ibfk_1` FOREIGN KEY (`serverId`) REFERENCES `servers` (`serverId`),
  CONSTRAINT `playerjoins_ibfk_2` FOREIGN KEY (`playerId`) REFERENCES `players` (`playerId`)
) ENGINE=InnoDB AUTO_INCREMENT=64983 DEFAULT CHARSET=latin1

CREATE TABLE `playerleaves` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `globalId` int(11) NOT NULL,
  `date` datetime NOT NULL,
  `serverId` int(11) NOT NULL,
  `playerId` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `globalId` (`globalId`),
  KEY `date` (`date`),
  KEY `serverId` (`serverId`),
  KEY `playerId` (`playerId`),
  CONSTRAINT `playerleaves_ibfk_1` FOREIGN KEY (`serverId`) REFERENCES `servers` (`serverId`),
  CONSTRAINT `playerleaves_ibfk_2` FOREIGN KEY (`playerId`) REFERENCES `players` (`playerId`)
) ENGINE=InnoDB AUTO_INCREMENT=45676 DEFAULT CHARSET=latin1

现在问题:

我希望将查询部分(1)与部分(2)和(3)组合在一起,使levelsloaded中的行仅在below.origin = 'playerjoin' AND above.origin = 'playerleave'时返回。结合表格和IF - 部分。

,我现在陷入困境

1 个答案:

答案 0 :(得分:0)

虽然这不是最有效的方法,但你可以这样做。

SELECT id, globalId, date, serverId, gamemodeId, mapId 
FROM levelsloaded l
    JOIN above a ON 1 = 1
    JOIN below b ON 1 = 1
WHERE b.origin = 'playerjoin' AND a.origin = 'playerleave'