如何在sql中连接两个不相关的表 [英] How to join two unrelated tables in sql

查看:61
本文介绍了如何在sql中连接两个不相关的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张桌子:

表 1:公式

FormulaId    Formula Text
1            [Qty] * [Rect]
2            [Qty] * [Al]
3            [Mt] * [Cat]  

表 2:上下文

ContextId    Name
1            Test 1
2            Test 2
3            Test 3
4            Test 4    

我需要以某种方式在 sql server 2008 R2 中加入那些以获得一个表格,其中对于每个上下文 ID,我将有一个完整的公式列表,即

I need to join those somehow in sql server 2008 R2 to get a table where for each context id I will have a full list of formulas, i.e.

结果

ContextId    Name     FormulaId    Formula Text    
1            Test 1   1            [Qty] * [Rect]
1            Test 1   2            [Qty] * [Al]
1            Test 1   3            [Mt] * [Cat]
2            Test 2   1            [Qty] * [Rect]
2            Test 2   2            [Qty] * [Al]
2            Test 2   3            [Mt] * [Cat]
3            Test 3   1            [Qty] * [Rect]
3            Test 3   2            [Qty] * [Al]
3            Test 3   3            [Mt] * [Cat]
4            Test 4   1            [Qty] * [Rect]
4            Test 4   2            [Qty] * [Al]
4            Test 4   3            [Mt] * [Cat]

推荐答案

您想使用 CROSS JOIN:

SELECT FormulaId, Formula, ContextId, [Name]
FROM Formula
CROSS JOIN Context

这篇关于如何在sql中连接两个不相关的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆