0

如果我想检查三个不同数据库中的任何一个中的 3 个不同表中存在哪些员工(用字母表示)。我的查询应该是什么样子?我不想选择整个表格并将其粘贴到 excel 中,然后进行 vlookup。实际上我有 13 个数据库需要加入,但我在问题中只使用了三个示例数据库来解释我的困难是什么。我只需要知道我的查询应该是什么样子。我正在考虑使用子查询将我的表连接到 12 个数据库中。请帮忙。

    Database1.ResourceTableUS  Database2.ResourceTableUK     Database3.ResourceTableAUS
col  Employee                   Employee                      Employee

row1  A                         A                             A                   
row2  B                         C                             B
row3  C                         I                             C
row4  E                         J                             K
row5  F                         K                             N
row6  H                         N                             T

Answer: A,B,C,K,N - because these are present at least in two tables (B) or three tables (A) in three databases.

谢谢!

4

2 回答 2

0

请试试:

select DISTINCT
    COLUMN_Name
from
    select 
        COLUMN_Name, COUNT(*) over (partition by COLUMN_Name) Num
    FROM(
        select COLUMN_Name from Database1.ResourceTableUS union all
        select COLUMN_Name from Database2.ResourceTableUK union all
        select COLUMN_Name from Database3.ResourceTableAUS
    )x
)xx 
where Num>1
于 2013-10-31T11:41:13.593 回答
0
SELECT A.employeeID FROM
(select * FROM Database1.ResourceTableUS,  Database2.ResourceTableUK,  Database3.ResourceTableAUS
 WHERE Database1.ResourceTableUS.employeeID IN (Database2.ResourceTableUK.employeeID,
 Database3.ResourceTableAUS.employeeID) AND Database2.ResourceTableUK.employeeID IN
 (Database1.ResourceTableUS.employeeID,Database3.ResourceTableAUS.employeeID) AND 
 Database3.ResourceTableAUS.employeeID  IN (Database2.ResourceTableUK.employeeID,Database1.ResourceTableUS.employeeID)) A
于 2013-10-31T11:50:02.967 回答