连接两个匿名表。

时间:2021-03-25 19:14:39

Following query runs:

下面的查询:

select foo.g from (select 'hello' as g) as foo

and this query runs aswell:

这个查询也可以运行:

select bla.x from (select 'world' as x) as bla

but this one does not:

但是这个没有:

select * from (select foo.g from (select 'hello' as g) as foo) join (select bla.x from (select 'world' as x) as bla)

I get the following error message:

我得到以下错误信息:

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'join'.

Why is the error ? Is it possible to join these tables somehow?

为什么会出现错误?是否可能以某种方式加入这些表?

3 个解决方案

#1


2  

Give names to you tables using As <temp_table_name> and you need to specify the column two tables are joining ON. Since there is no column to join ON i used a tautology (which will always result True) assuming that the result you expect is: hello , world in two columns

使用As 来给您的表命名,您需要指定列两个表的连接。由于没有要连接的列,所以我使用了重言式(它总是结果为True),假设您期望的结果是:hello, world在两个列中

Here is the rearranged query:

以下是重新排列的查询:

select * 
from 
(
    select foo.g 
    from 
    (
        select 'hello' as g
    ) as foo
) As t1
inner join 
(
    select bla.x 
    from (select 'world' as x) as bla
) As t2 ON 1 = 1

#2


2  

You need to give alias names to your tables. The below code works

您需要为您的表提供别名。下面的代码能够工作

select * from (select foo.g from (select 'hello' as g) as foo) T1
join (select bla.x from (select 'world' as x) as bla) T2 on t1.g=t2.x

#3


1  

The error was due the missing the ON keyword. The ON clause works like the WHERE clause and is where you add your joining filter.

错误是由于缺少了ON关键字。ON子句的工作方式与WHERE子句类似,是添加连接过滤器的地方。

Its not directly related to the lack of names and aliases on the tables and columns, but if you don't name your columns and tables the ON clause can not know what do you want as result and how do you want to join tables on

它与表和列上缺少名称和别名没有直接关系,但是如果您不给列和表命名,on子句就不知道您想要的结果是什么,以及您希望如何连接表

#1


2  

Give names to you tables using As <temp_table_name> and you need to specify the column two tables are joining ON. Since there is no column to join ON i used a tautology (which will always result True) assuming that the result you expect is: hello , world in two columns

使用As 来给您的表命名,您需要指定列两个表的连接。由于没有要连接的列,所以我使用了重言式(它总是结果为True),假设您期望的结果是:hello, world在两个列中

Here is the rearranged query:

以下是重新排列的查询:

select * 
from 
(
    select foo.g 
    from 
    (
        select 'hello' as g
    ) as foo
) As t1
inner join 
(
    select bla.x 
    from (select 'world' as x) as bla
) As t2 ON 1 = 1

#2


2  

You need to give alias names to your tables. The below code works

您需要为您的表提供别名。下面的代码能够工作

select * from (select foo.g from (select 'hello' as g) as foo) T1
join (select bla.x from (select 'world' as x) as bla) T2 on t1.g=t2.x

#3


1  

The error was due the missing the ON keyword. The ON clause works like the WHERE clause and is where you add your joining filter.

错误是由于缺少了ON关键字。ON子句的工作方式与WHERE子句类似,是添加连接过滤器的地方。

Its not directly related to the lack of names and aliases on the tables and columns, but if you don't name your columns and tables the ON clause can not know what do you want as result and how do you want to join tables on

它与表和列上缺少名称和别名没有直接关系,但是如果您不给列和表命名,on子句就不知道您想要的结果是什么,以及您希望如何连接表