表格1:
Person_ID Name Salary_Revisions
1 Test1 100
1 Test1 200
2 Test2 300
2 Test2 400
表2:
Person ID Department
-------------------------- ----------------
1 Physics
1 Chemistry
2 Maths
我想得到如下结果:
Person_ID Name Salary_Revisions Department
--------------------- ------------------ ---------------------- --------------
1 Test1 100 Physics
1 Test1 200 Chemistry
2 Test2 300 Maths
2 Test2 400
实际的:
Person ID Name Salary Revisions Department
------------------ --------- --------------------- ----------------
1 Test1 100 Physics
1 Test1 200 Physics
1 Test1 100 Chemistry
1 Test1 200 Chemistry
2 Test2 300 Maths
2 Test2 400 Maths
你能帮我实现预期的结果吗?
在实现这一点时,我通过使用人员 ID 将表 1 与表 2 左连接来编写了一个存储过程。通过在数据库中执行查询它返回像实际结果一样。
SQL查询:
SELECT table1.person_ID, table1.name, table1.salary_revisions, table2.department
from table1
left outer join table2 on table1.person_id=table2.person_id