left join returns null 时创建默认值
Create a default value when left join returns null
我有这样的查询
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 = ''
结果是这样的。我想问的是,有没有办法为那些 NULL 列设置默认值?
您可以在 select 查询中使用 ISNULL()
。
例如;我在 MerchantId 为 null 时设置为 0,在 MidName 为 null 时设置为空字符串
ISNULL(MerchantId, 0)
ISNULL(MidName, '')
在主查询中执行此操作。
例如;
select ISNULL(m.MerchantId, 0), ISNULL(Mid.MidName, '')
from Users u
我有这样的查询
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 = ''
结果是这样的。我想问的是,有没有办法为那些 NULL 列设置默认值?
您可以在 select 查询中使用 ISNULL()
。
例如;我在 MerchantId 为 null 时设置为 0,在 MidName 为 null 时设置为空字符串
ISNULL(MerchantId, 0)
ISNULL(MidName, '')
在主查询中执行此操作。
例如;
select ISNULL(m.MerchantId, 0), ISNULL(Mid.MidName, '')
from Users u