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

时间:2009-05-13 08:31:24

标签: tsql permutation

假设我有两个子查询:

SELECT Id AS Id0 FROM Table0

=>

Id0
---
1
2
3

and

SELECT Id AS Id1 FROM Table1

=>


Id1
---
4
5
6

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

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

3 个答案:

答案 0 :(得分:1)

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

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

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

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

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

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

答案 1 :(得分:1)

试试这个:

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

格雷

答案 2 :(得分:1)

选择     Table0.Id0,     Table1.Id1 从     Table0 完全加入Table1上     1 = 1