如何从T-SQL中的两个子查询中获取项目的排列?

时间:2022-10-06 21:10:49

Lets say I have two subqueries:

可以说我有两个子查询:

SELECT Id AS Id0 FROM Table0

=>

Id0
---
1
2
3

and

SELECT Id AS Id1 FROM Table1

=>


Id1
---
4
5
6

How do I combine these to get the query result:

如何组合这些以获取查询结果:

Id0 Id1
-------
1   4
1   5
1   6
2   4
2   5
2   6
3   4
3   5
3   6

3 个解决方案

#1


Try this :

试试这个 :

SELECT A.Id0, B.Id1
FROM (SELECT Id AS Id0 FROM Table0) A, 
     (SELECT Id AS Id1 FROM Table1) B

Grégoire

#2


Cartesian join, a join with no join condition

笛卡尔连接,没有连接条件的连接

select id0.id as id0, id1.id as id1 
from id0, id1

alternatively you can use the CROSS JOIN syntax if you prefer

或者,如果您愿意,也可以使用CROSS JOIN语法

select id0.id as id0, id1.id as id1 
from id0 cross join id1

you can order your query if you want a specific order, from your example it looks like you want

如果您想要特定订单,可以订购查询,从您的示例中看起来就像您想要的那样

select id0.id as id0, id1.id as id1
from id0 cross join id1 order by id0.id, id1.id

#3


SELECT Table0.Id0, Table1.Id1 FROM Table0 Full Join Table1 on 1=1

SELECT Table0.Id0,Table1.Id1 FROM Table0 Full Join Table1 on 1 = 1

#1


Try this :

试试这个 :

SELECT A.Id0, B.Id1
FROM (SELECT Id AS Id0 FROM Table0) A, 
     (SELECT Id AS Id1 FROM Table1) B

Grégoire

#2


Cartesian join, a join with no join condition

笛卡尔连接,没有连接条件的连接

select id0.id as id0, id1.id as id1 
from id0, id1

alternatively you can use the CROSS JOIN syntax if you prefer

或者,如果您愿意,也可以使用CROSS JOIN语法

select id0.id as id0, id1.id as id1 
from id0 cross join id1

you can order your query if you want a specific order, from your example it looks like you want

如果您想要特定订单,可以订购查询,从您的示例中看起来就像您想要的那样

select id0.id as id0, id1.id as id1
from id0 cross join id1 order by id0.id, id1.id

#3


SELECT Table0.Id0, Table1.Id1 FROM Table0 Full Join Table1 on 1=1

SELECT Table0.Id0,Table1.Id1 FROM Table0 Full Join Table1 on 1 = 1