Sql server 当left join返回null时创建默认值
我有一个这样的问题Sql server 当left join返回null时创建默认值,sql-server,left-join,isnull,Sql Server,Left Join,Isnull,我有一个这样的问题 select * from Users u left join( select ContactInformationId, Address, City, StateProvince,
select *
from Users u left join(
select ContactInformationId,
Address,
City,
StateProvince,
CountryId,
ZipCode,
PrimaryContactNumber,
Fax,
MobileNumber
from ContactInformation
) c on u.ContactInformationId = c.ContactInformationId
left join (
select ISNULL(MerchantId, @MerchantId) as MerchantId,
ISNULL(MerchantName, @MerchantName) as MerchantName,
ISNULL(MerchantEmail, @MerchantEmail) as MerchantEmail,
ISNULL(ContactInformationId, @ContactInformationId) as ContactInformationId
from Merchants
) m on c.ContactInformationId = m.ContactInformationId
left join (
select IsNull(MidId, 0) as MidId,
IsNull(MidName, '') as MidName,
IsNull(MerchantId, 0) as MerchantId,
IsNull(Param_2, '') as Param_2,
IsNull(Param_6, '') as Param_6
from Mids
) MID on m.MerchantId = MID.MerchantId
where u.FirstName = '' and u.LastName = '' or u.MiddleName = ''
结果是这样的。我想问的是,有没有办法为这些空列设置默认值?您可以在select查询中使用ISNULL()
eg;当MerchantId为null时,我将其设置为0;当MidName为null时,我将其设置为空字符串
ISNULL(MerchantId, 0)
ISNULL(MidName, '')
在主查询中执行此操作
eg
您可以在select查询中使用ISNULL()
eg;当MerchantId为null时,我将其设置为0;当MidName为null时,我将其设置为空字符串
ISNULL(MerchantId, 0)
ISNULL(MidName, '')
在主查询中执行此操作
eg
嗨,我真的试过了,但结果和我贴的图片一样。知道为什么吗?你需要在主查询中进行,而不是在子查询中。嗨,我确实尝试过,但结果与我发布的图像相同。知道为什么吗?您需要在主查询中执行,而不是在子查询中。