在加入期间,在其自己的嵌套选择语句中使用Select的表中使用表
这可能是对问题的重复或不良描述,对不起。是否可以在JOIN中的嵌套选择语句中从以下查询中使用 sub ,如下所示:
SELECT
*
FROM
(
SELECT
several_values_from_several_tables
FROM
table1 t1
JOIN multiple_tables ON t1_and_eachother
WHERE
several_conditions_including_nested_select
ORDER BY
multiple_columns
) sub
INNER JOIN (
SELECT
some_id, MAX(some_time_value) AS max_time
FROM
sub
GROUP BY
some_id
) sub2 ON sub.some_id = sub2.some_id
WHERE
sub.time = sub2.time
我想在JOIN SELECT STACT中使用Sub,以免重复相同的选择语句由于它很大且昂贵(我在不同的时间戳上也有第二个连接,这将导致进行3次相同昂贵的查询)。我猜想直到执行加入和将执行子句的位置才能创建SUB。如果有人找到了解决方案或解决方法,以在单个查询中获得相同的结果,然后再欣赏一些指示。
This might be a duplicate or poor description of the problem, sorry if so. Is it possible to use sub from the following query in a nested select statement in the join as shown below:
SELECT
*
FROM
(
SELECT
several_values_from_several_tables
FROM
table1 t1
JOIN multiple_tables ON t1_and_eachother
WHERE
several_conditions_including_nested_select
ORDER BY
multiple_columns
) sub
INNER JOIN (
SELECT
some_id, MAX(some_time_value) AS max_time
FROM
sub
GROUP BY
some_id
) sub2 ON sub.some_id = sub2.some_id
WHERE
sub.time = sub2.time
I'd like to use sub in the joins select statement to avoid having to repeat the same select statement as it is quite large and expensive (I also have a second join on a different timestamp which would result in doing the same expensive query 3 times). I guess that sub is not created until joins are performed and where clause is to be executed. If anyone has found a solution or workaround to achieve the same result in a single query before I'd greatly appreciate some pointers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CTE(一个常见的表表达式,又称保理子句)可以使用,例如
CTE (a Common Table Expression, a.k.a. the WITH factoring clause) can be used for that, e.g.
在这种情况下,您应该避免自加入并使用分析函数:
注意:您还可以使用
等级
或dense_rank
分析功能而不是Max
。In this case, you should avoid the self-join and use an analytic function:
Note: You could also use the
RANK
orDENSE_RANK
analytic functions instead ofMAX
.