如何将查询的输出存储到临时表中并在新查询中使用该表? [英] How can I store the output of a query into a temporary table and use the table in a new query?
问题描述
我有一个MySQL查询,该查询使用带有2个内部联接的3个表.然后,我必须从此查询输出中找到一组的最大值.将两者结合起来超出了我的范围.我可以通过将第一个复杂查询的输出存储到某种临时表中,给它命名,然后在新查询中使用该表来解决这个问题吗?这将使代码更易于管理.谢谢您的帮助.
I have a MySQL query which uses 3 tables with 2 inner joins. Then, I have to find the maximum of a group from this query output. Combining them both is beyond me. Can I break down the problem by storing the output of the first complicated query into some sort of temporary table, give it a name and then use this table in a new query? This will make the code more manageable. Thank you for your help.
推荐答案
这非常简单:
CREATE TEMPORARY TABLE tempname AS (
SELECT whatever, whatever
FROM rawtable
JOIN othertable ON this = that
)
关闭连接后,临时表将消失.临时表包含创建时捕获的数据.
The temporary table will vanish when your connection closes. A temp table contains the data that was captured at the time it was created.
您也可以像这样创建视图.
You can also create a view, like so.
CREATE VIEW viewname AS (
SELECT whatever, whatever
FROM rawtable
JOIN othertable ON this = that
)
视图是永久对象(当连接关闭时它们不会消失),但是它们在您调用它们时从基础表中检索数据.
Views are permanent objects (they don't vanish when your connection closes) but they retrieve data from the underlying tables at the time you invoke them.
这篇关于如何将查询的输出存储到临时表中并在新查询中使用该表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!