重复的临时表MySQL

时间:2016-01-21 11:22:11

标签: mysql mysqli

我想在同一个查询中多次引用一个临时表。由于临时表的问题我不能这样做:

http://dev.mysql.com/doc/refman/5.7/en/temporary-table-problems.html

所以要修复我正在创建临时表的重复项:

CREATE TEMPORARY TABLE IF NOT EXISTS earnings_temp_one AS (SELECT * FROM earnings WHERE earning_account_id = ?);
CREATE TEMPORARY TABLE IF NOT EXISTS earnings_temp_two AS (SELECT * FROM earnings WHERE earning_account_id = ?);
CREATE TEMPORARY TABLE IF NOT EXISTS earnings_temp_three AS (SELECT * FROM earnings WHERE earning_account_id = ?);

但这需要我查询大量的桌面收益3次,这在某种程度上违背了目的。

我有没有办法以不同的名称复制临时表?

然后我就可以在大桌子上运行一次查询。

或许可以修复临时表问题,不需要我创建重复项?

1 个答案:

答案 0 :(得分:5)

好的,我解决了它:

CREATE TEMPORARY TABLE IF NOT EXISTS earnings_temp_one AS (SELECT * FROM earnings WHERE earning_account_id = ?);
CREATE TEMPORARY TABLE IF NOT EXISTS earnings_temp_two AS (SELECT * FROM earnings_temp_one);
CREATE TEMPORARY TABLE IF NOT EXISTS earnings_temp_three AS (SELECT * FROM earnings_temp_one);