简体   繁体   English

从两个表中选择并设置空列

[英]Select from two tables and set null column

I have two tables 我有两张桌子

Schedule: 时间表:

scheduleID    DriverID      ScheduleDate
11            1             2015-05-20
22            2             2015-05-20
33            NULL          2015-05-21
44            NULL          2015-05-21

and

Employee: 雇员:

ID           Fname          Lname
1            jack           miller
2            kelly          moore
3            mark           sam
4            tom            hanks

I want to select from these two tables based on ScheduleDate and show the driver full name from Employee table and show null columns. 我想从基于ScheduleDate的两个表中进行选择,并从Employee表中显示驱动程序全名,并显示空列。

The result should be like: 结果应为:

scheduleID    Fname       Lname
33            NULL         NULL
44            NULL         NULL

I used: 我用了:

select  row_number() over (order by scheduleID desc) as schedule, scheduleID, Driver_ID,FirstName,LastName
From Schedule, Employee
where scheduleDate= '2015-05-21' AND EmployeID=Driver_ID;

It works well if " scheduleDate= '2015-05-20' " 如果“ scheduleDate ='2015-05-20'”效果很好

but I need to show null Columns too! 但我也需要显示空列! any help! 任何帮助!

You need a left join for this -- just emphasizing why you should never using implicit join syntax. 为此,您需要left join -只是强调为什么不应该使用隐式join语法。 Simple rule: Always use explicit join syntax. 简单规则: 始终使用显式join语法。 Never use commas in the from clause. 请勿在from子句中使用逗号。

select row_number() over (order by scheduleID desc) as schedule, scheduleID, 
       Driver_ID, FirstName, LastName
From Schedule left join
     Employee
     on EmployeID = Driver_ID
where scheduleDate = '2015-05-21';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM