如果要连接的列为空,连接仍然有效吗?
我有这个存储过程,当我添加联接时,我没有得到任何结果。这是因为用于连接条件的列 ForClientID 为空吗?这会影响表的结果吗?如果我选择带有 Appointment.ForClientID 而不是 Client.Name 的表,则表的其余部分会加载 ForClientID 列为空的情况。预先感谢,这是一个非常有帮助的社区。
Create procedure GetAppointmentsByProfessionalName(@ProfessionalName varchar(256))
as
declare @ProfessionalID uniqueidentifier
set @ProfessionalID = (select UserId from aspnet_Users where UserName = @ProfessionalName)
select Appointment.AppointmentID as 'Appointment ID',
Client.Name as 'Client Name',
CONVERT(VARCHAR(10),Appointment.ProposedDate,111) as 'Date',
CONVERT(CHAR(8), Appointment.ProposedTime, 114)as 'Time',
Appointment.ClientDescription as 'Client Notes',
Appointment.Confirmed as 'Confirmed Appointment'
from Appointment join Client on Client.ClientID = Appointment.ForClientID
where Appointment.ForProfessionalID = @ProfessionalID
go
I've got this stored procedure, and when I add the join I get no results. Is this because the column used for the join condition, ForClientID, is null? Would that affect the results of the table? If i select the table with Appointment.ForClientID instead of Client.Name, the rest of the table loads with the ForClientID column being null. Thanks in advance, this is an amazingly helpful community.
Create procedure GetAppointmentsByProfessionalName(@ProfessionalName varchar(256))
as
declare @ProfessionalID uniqueidentifier
set @ProfessionalID = (select UserId from aspnet_Users where UserName = @ProfessionalName)
select Appointment.AppointmentID as 'Appointment ID',
Client.Name as 'Client Name',
CONVERT(VARCHAR(10),Appointment.ProposedDate,111) as 'Date',
CONVERT(CHAR(8), Appointment.ProposedTime, 114)as 'Time',
Appointment.ClientDescription as 'Client Notes',
Appointment.Confirmed as 'Confirmed Appointment'
from Appointment join Client on Client.ClientID = Appointment.ForClientID
where Appointment.ForProfessionalID = @ProfessionalID
go
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
围绕 NULL 的行为是不同类型 JOIN 之间的主要区别。阅读 http://en.wikipedia.org/wiki/Join_%28SQL%29 ,虽然是维基百科,但它是关于该主题的令人惊讶的好文章。
The behavior surrounding NULLs is the primary difference between the different types of JOIN. Read up on http://en.wikipedia.org/wiki/Join_%28SQL%29 , which while being Wikipedia is a surprisingly good article on the topic.
这是使用
OUTER JOIN
进行重写,以考虑Appointment.ForClientID
为NULL
并替换局部变量@ProfessionalID
与INNER JOIN
:Here's a re-write using an
OUTER JOIN
to take account of theAppointment.ForClientID
beingNULL
-able and replacing the local variable@ProfessionalID
with anINNER JOIN
: