正如标题所说,我可以在 MySQL 中预定义一些结果集(因为它是从内层到外层进行评估),稍后在查询中重用它们吗?
我在这里有一个示例查询(不要被大小吓到):
SELECT id, globalId, date, serverId, gamemodeId, mapId FROM
(
(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
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
ORDER BY ll.date DESC
) above
WHERE origin = 'playerjoins')
UNION ALL
(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
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
ORDER BY ll.date DESC
) below
WHERE origin = 'playerleaves')
) total
ORDER BY date DESC
我想以某种方式预定义pjl
:
(
(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
我在完整查询中使用了两次。我在查询开始时想象一下
pjl = (...)
,然后在其余代码中,您可以使用pjl
。它应该仅在查询的生命周期内有效。
如果在PHP / MySQL设置中使用多行查询(使用;
)定义,我可以看到发生的一些问题。
答案 0 :(得分:0)
您可以使用MySQL中的过程(或可能是函数)来完成此操作:http://dev.mysql.com/doc/refman/5.1/en/create-procedure.html或http://dev.mysql.com/doc/refman/5.1/en/stored-routines.html