我试图显示表1包含的所有记录,每个记录以及表2中的所有记录:
Table 1
company adress
------------------
A AdressX
B AdressY
C AdressZ
Table 2
Product Price
----------------
P1 50
P2 60
Result :
company Product
----------------
A P1
A P2
B P1
B P2
C P1
C P2
答案 0 :(得分:2)
那将是交叉联接。
SELECT t1.company,
t2.product
FROM [table 1] t1
CROSS JOIN [table 2] t2
ORDER BY t1.company,
t2.product;
答案 1 :(得分:1)
使用CROSS APPLY
可以实现此预期输出:
SELECT T1.company, T2.Product
FROM Table1 T1
CROSS APPLY Table2 T2
ORDER BY T1.company, T2.Product
包含示例数据的演示
DECLARE @Table1 TABLE (company VARCHAR (1), adress VARCHAR (10));
INSERT INTO @Table1 (company, adress) VALUES
('A', 'AdressX'),
('B', 'AdressY'),
('C', 'AdressZ');
DECLARE @Table2 TABLE (Product VARCHAR (2), Price INT);
INSERT INTO @Table2 (Product, Price) VALUES
('P1', 50),
('P2', 60);
SELECT T1.company, T2.Product
FROM @Table1 T1
CROSS APPLY @Table2 T2
ORDER BY T1.company, T2.Product
输出:
company Product
---------------
A P1
A P2
B P1
B P2
C P1
C P2
答案 2 :(得分:0)
以下查询将从两个表中选择所有数据(未链接或相关)。
SELECT *
FROM TABLE 1
OUTER APPLY
(
SELECT *
FROM Table 2
) AS Table 2
如果有外键,则可以使用LEFT JOIN (SELECT)