[英]How to combine two columns from different tables that have a similar name but have different values in SQL Server
I have three tables ( example ) STAFF, STU, EMP
. 我有三个表( 示例 )
STAFF, STU, EMP
。
I want to combine the column EMPID
in table STAFF
and table EMP
into 1 column? 我想将表
STAFF
和表EMP
的列EMPID
合并为1列?
My previous query is like this, 我以前的查询是这样的,
SELECT *
FROM STU s
FULL OUTER JOIN STAFF st ON st.STAFFID = STUID
FULL OUTER JOIN EMP e ON s.STUID = st.EMPID
The result is like this 结果是这样的
The expected result is just like the above screenshot, but I want to join EMPID into one column only. 预期的结果与上面的屏幕截图一样,但是我只想将EMPID加入一列。
UPDATE : 更新 :
I tried using this query: 我尝试使用此查询:
SELECT
stu.stuid, stu.stuname, stu.stucode,
s.staffid, s.staffname, s.staffcode,
emp.empname, emp.empcode,
COALESCE (emp.empid, staff.staffid) AS col
FROM
STU, Staff, EMP
FULL OUTER JOIN
STAFF s ON s.STAFFID = stu.STUID
FULL OUTER JOIN
EMP e ON stu.STUID = s.EMPID
but it displays an error like this 但它显示这样的错误
Use below query to get the desired result. 使用以下查询可获得所需的结果。
SELECT s.StuID, s.StuName, s.Stucode, st.StaffId, st.StaffName, st.Staffcode, isnull(st.EmpId, e.EmpId) EmpId, e.EmpCode, e.EmpName
FROM STU s FULL outer JOIN
STAFF st
ON st.STAFFID = STUID FULL OUTER JOIN
EMP e
ON s.STUID = st.EMPID
Note: You will get the one emp Id column as needed. 注意:您将根据需要获得一个emp Id列。 If Staff emp id is not null then staff emp id will be displayed else employee emp id will be displayed
如果职员emp id不为null,则将显示职员emp id,否则将显示职员emp id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.