选择所有数据,包括另一个表,即使为null [英] Select all data include another table even if null

查看:66
本文介绍了选择所有数据,包括另一个表,即使为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置: (1)员工表(employeeID,firstName,lastName)

Setup: (1) Employee table (employeeID, firstName, lastName)

(1)鞋桌(shoeID,Employee_employeeID,shoeName,shoeColor,shoeBrand)

(1) Shoe table (shoeID, Employee_employeeID, shoeName, shoeColor, shoeBrand)

我想选择Employee表中的所有行,即使在热靴表中没有匹配的EmployeeID(Employee_EmployeeID)仍然显示整个行.所需输出示例:

I want to select all rows in Employee table and even if there is no matching EmployeeID (Employee_EmployeeID) in the shoe table display that entire row anyway. Example desired output:

 EmployeeID  | firstName    | lastName    | shoeName     |   shoeColor  | shoeBrand

 1            John           Smith         AirMax2          Red          Nike
 2            Ronald         Mcdonald      null             null         null
 3            James          Knight        null             null         null
 4            Cindy          Smith         Pump             Brown        Cole Haan

我尝试了很多不同的联接,每个员工都会得到重复的行.

I have tried a lot of different joins and I will get duplicate rows for each Employee.

推荐答案

尝试一下

   SELECT e.employeeId,e.firstName,e.lastName,s.ShoeName,s.ShoeColor,s.ShoeBrand 
   FROM    Employee e
   LEFT JOIN Shoe s
   ON e.employeeID = s.Employee_employeeID

此处演示SQLFIDDLE

这篇关于选择所有数据,包括另一个表,即使为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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