我遇到SQL连接问题。
我有EMPLOYEE
EmpID, FirstName, LastName, Email, Phone
我有另一张表OTHERNAME
,其中包含2个字段"Name" & "OtherName"
。
此表包含查询值,例如"James", "Jim"; "Thomas", "Tom"; "Steven", "Steve"
。
我想编写一个返回行的查询
EmpID, FirstName, LastName, Email, Phone, OtherName
where Employee.Firstname = OTHERName.Name
答案 0 :(得分:7)
Select e.EmpID, e.FirstName, e.LastName, e.Email, e.Phone, o.OtherName
From Employee e
Left Outer Join OtherName o on e.FirstName = o.Name
从您的评论中,您实际上想要一个外部联接。
(来自评论)外部联接将返回所有员工,如果有其他名称则返回其他名称,否则其他名称将是您可以在代码中处理的空值。内部联接将结果限制为仅具有匹配记录的员工。
答案 1 :(得分:1)
试试这个:
SELECT
e.EmpID
CASE
WHEN o.OtherName IS NOT NULL THEN OtherName
ELSE e.FirstName
END AS FirstName
,e.LastName
,e.Email
,e.Phone
,o.OtherName
FROM Employee e
LEFT OUTER JOIN OtherName o ON e.FirstName = o.Name
答案 2 :(得分:1)
select e.EmpID, e.FirstName, e.LastName, e.Email, e.Phone, o.OtherName
from employee e,othername o
where e.FirstName=o.name
答案 3 :(得分:0)
SELECT E.EmpID, E.FirstName, E.LastName, E.Email, E.Phone, O.OtherName
FROM Employee E
INNER JOIN Othername O
ON E.Firstname = O.Name
应该做的伎俩
或者,如果您想要所有结果,即使是那些没有“其他名称”值的结果。
SELECT E.EmpID, E.FirstName, E.LastName, E.Email, E.Phone, O.OtherName
FROM Employee E
LEFT OUTER JOIN Othername O
ON E.Firstname = O.Name
答案 4 :(得分:0)
SELECT E.EmpId, E.FirstName, E.LastName, E.Email, E.Phone, O.OtherName 来自EMPLOYEE E. INNER JOIN OTHERNAME O. ON E.FirstName = O.Name
答案 5 :(得分:0)
您可以使用以下SQL命令
SELECT EMPLOYEE.EmpID,
EMPLOYEE.FirstName,
EMPLOYEE.LastName,
EMPLOYEE.Email,
EMPLOYEE.Phone,
OTHERNAME.OtherName FROM EMPLOYEE INNER JOIN OTHERNAME ON EMPLOYEE.FirstName = OTHERNAME.Name