从一个表中返回系列,每个结果在另一个表中

时间:2017-05-03 18:00:26

标签: sql ms-access

我正在努力寻找这个问题的解决方案,因为我似乎无法找到正确的解决方法。

假设我有一个带有主管姓名的表tbl_Users

Employee     | Type
------------ | ----------
Supervisor 1 | Specialist
Supervisor 2 | Analyst
Supervisor 3 | Specialist
Supervisor 4 | Specialist

另一个有员工姓名的表tbl_Auditable

Employee
----------
Employee 1
Employee 2
Employee 3

这两个是独占的 - tbl_Users表中不存在tbl_Auditable中的名称。如何创建列表,以便tbl_Auditable中每一位tbl_Users的每位员工都重复一次?

Specialist   | Employee
------------ | ----------
Supervisor 1 | Employee 1
Supervisor 1 | Employee 2
Supervisor 1 | Employee 3
Supervisor 3 | Employee 1
Supervisor 3 | Employee 2
Supervisor 3 | Employee 3
Supervisor 4 | Employee 1
Supervisor 4 | Employee 2
Supervisor 4 | Employee 3

2 个答案:

答案 0 :(得分:1)

使用CROSS JOIN

结果是表1中的所有行都乘以表2中的所有行。

https://technet.microsoft.com/en-us/library/ms190690(v=sql.105).aspx

SELECT u.Employee AS Supervisor, a.Employee
FROM tbl_Users u
CROSS JOIN tbl_Auditable a

这样的事情应该有效。

答案 1 :(得分:1)

您想要“交叉加入”。这可以这样做:

select t1.Employee as Specialist, t2.Employee
from tbl_users as t1, tbl_Auditable t2
where t1.type = 'Specialist'
order by t1.Employee, t2.Employee