SQL连接相关问题 [英] SQL join related question

查看:97
本文介绍了SQL连接相关问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了SQL连接问题.

I am having trouble with a SQL join question.

我有一个表EMPLOYEEEmpID, FirstName, LastName, Email, Phone

我还有另一个具有两个字段"Name" & "OtherName"的表OTHERNAME.

I have another table OTHERNAME with 2 fields "Name" & "OtherName".

此表包含诸如"James", "Jim"; "Thomas", "Tom"; "Steven", "Steve"的查找值.

This table contains lookup values such as "James", "Jim"; "Thomas", "Tom"; "Steven", "Steve".

我想编写一个查询,该查询将返回行

I want to write a query which will return rows

EmpID, FirstName, LastName, Email, Phone, OtherName
where Employee.Firstname = OTHERName.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

从您的评论看来,您实际上想要一个外部联接.

From your comments it sounds like you actually want an outer join.

(来自注释)外部联接将返回所有雇员,如果有一个雇员,还将返回另一个雇员姓名,否则其他雇员姓名将为空值,您可以在代码中处理该空值.内部联接将结果限制为仅具有匹配记录的员工.

(From Comments) An outer join would return all employees, along with an Othername if there is one, otherwise Othername would be a null value which you could handle in code. An inner join limits the results to only employees with a matching record.

这篇关于SQL连接相关问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆