简体   繁体   English

如何在基础的select-join语句中使用create table语句的列别名?

[英]How to use column alias of create table statement in underlying select-join statement?

I've run into a minor problem with a query I want to build. 我遇到一个要构建的查询的小问题。 The idea is to construct a new table based on a join on the same old table where some kind of "expensive" calculation is done. 想法是在相同的旧表上基于联接构造一个新表,在该旧表上进行某种“昂贵”的计算。

The problem 问题

I can not use the calculated result (alias) of the field list in the where (or on) clause of the underlying select. 我不能在基础选择的where(或on)子句中使用字段列表的计算结果(别名)。

Why not calculate it again? 为什么不再次计算呢?

  • I would like to avoid calculating the result again as it is a 1.5 Mio entry table and I join about everything. 我想避免再次计算结果,因为它是一个1.5 Mio的条目表,我加入了所有内容。 There are some more conditions in the on clause but it's not really making a difference in number of operations. on子句还有更多条件,但实际上并没有改变操作数量。 Therefore I'd rather not do it twice per join. 因此,我宁愿每个联接都不做两次。
  • I just wanna now if it is possible :) 我现在想如果可能的话:)

SQL Statement SQL语句

CREATE TABLE `newTable` AS (
    SELECT (*COMPUTATION*) AS `calculated` 
    FROM oldTable AS t1 
    JOIN oldTable as t2 ON (
        t1.user != t2.user
        AND *other conditions*
    )
    WHERE `calculated` < *some number*
);

I've already thought of using functions or something else, but I think it should be doable in one simple query. 我已经考虑过使用函数或其他方法,但是我认为它应该可以在一个简单的查询中使用。

Have you tried wrapping your inner query one more level?? 您是否尝试过将内部查询再包装一层?

CREATE TABLE `newTable` AS 
(   select PreQuery.Calculated
       from ( SELECT (*COMPUTATION*) AS `calculated` 
                 FROM oldTable AS t1 
                 JOIN oldTable as t2 ON 
                   ( t1.user != t2.user
                     AND *other conditions* 
                   ) ) PreQuery
       where PreQuery.Calculated < *some number*
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM