MySQL文档说:“你不能在同一个查询中多次引用TEMPORARY表。”
我知道之前有人问过这件事。但我无法找到以下具体解决方案。
我正在预选临时表
CREATE TEMPORARY TABLE preselection AS SELECT ...;
现在我想做一些(大约20个甚至30个)工会
(SELECT FROM preselection ...)
UNION
(SELECT FROM preselection ...)
UNION
......
UNION
(SELECT FROM preselection ...)
我可以制作20或30个预选副本,并在每个表上进行每个选择,但如果我理解正确,这与在UNION链中的每个SELECT中作为子查询调用上面的预选查询相同。
有没有办法解决这个问题?
问候,
克里斯
完整查询:
CREATE TEMPORARY TABLE preselection AS
(
SELECT id, title, chapter, date2, date, snid, max(score) FROM `movies`
WHERE
(
cluster is not NULL
)
AND
(
`date` <= '2012-02-20 05:20:00'
AND `date` > '2012-02-19 17:20:00'
AND (TIMEDIFF(date, date2) < '12:00:00')
)
GROUP BY cluster
)
UNION
(
SELECT id, title, chapter, date2, date, snid, score FROM `movies`
WHERE cluster IS NULL
AND
(
`date` <= '2012-02-20 05:20:00' AND `date` > '2012-02-19 17:20:00' AND (TIMEDIFF(date, date2) < '12:00:00')
)
);
(SELECT * FROM preselection WHERE snid=1 AND chapter LIKE '#A_OT%'
DESC LIMIT 4)
UNION
…
UNION
(SELECT * FROM preselection WHERE snid=19 AND chapter LIKE '#A_OT%'
LIMIT 4)
UNION
... for each chapter from A to J and every snid from 1 to 19 ...
UNION
(SELECT * FROM preselection WHERE snid=1 AND chapter LIKE '#J_OT%'
LIMIT 4)
UNION
…
UNION
(SELECT * FROM preselection WHERE snid=19 AND chapter LIKE '#J_OT%'
LIMIT 4)
ORDER BY `score` DESC, `date`;
答案 0 :(得分:4)
我认为错误信息很明确:您无法使用单个临时表执行此操作。创建数据视图而不是临时表是否可以解决问题?
答案 1 :(得分:1)
是的,这可能令人沮丧。我通常使用的解决方案涉及使用更复杂的查询或使用更多临时表来避免临时表。
简单的副本(但就像你提到的那样对你没用)
CREATE TEMPORARY TABLE preselectionCopy AS
SELECT * FROM preselection;
或保存结果的临时表
CREATE TEMPORARY TABLE result (
id INT NULL,
title VARCHAR(256) NULL
...
);
......可以像
一样简单CREATE TEMPORARY TABLE result AS
SELECT * FROM preselection WHERE snid=1 AND chapter LIKE '#A_OT%'
LIMIT 4;
INSERT INTO result
SELECT * FROM preselection WHERE snid=19 AND chapter LIKE '#A_OT%'
LIMIT 4;
...
SELECT * FROM result
ORDER BY `score` DESC, `date`;