gpt4 book ai didi

mysql - 为什么我不能再使用派生表的名称(子查询结果)?

转载 作者:行者123 更新时间:2023-11-29 10:42:48 32 4
gpt4 key购买 nike

我需要重用子查询生成的表,但是当我尝试执行这样的 SQL 时

SELECT id 
FROM company
LEFT JOIN
(
SELECT * FROM sales
-- join
-- join
-- join
-- join
) foo
ON foo.company_id = company.id

LEFT JOIN
(
SELECT company_id,balance FROM foo -- generates error
UNION ALL
SELECT company_id,balance FROM manual_moves
) moves
ON moves.company_id = company.id;
-- note: the actual sub-query has been omitted to make it focused on the error

不幸的是我遇到了错误

Table 'databaseName.foo' doesn't exist

构建foo表的子查询有很多连接,我不想再次重写相同子查询(我猜测这会对性能产生影响)

有没有办法可以再次重用子查询结果表-foo-的名称?

最佳答案

首先将“foo”查询的内容转储到临时表中,然后在主查询中查询该临时表。

关于mysql - 为什么我不能再使用派生表的名称(子查询结果)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45164132/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com