我如何选择如果San_Cliente.NomeComple为非空,那么San_Cliente.Excluido = 0?
现在这是我的SELECT
SELECT Key.Key_Id,
Key.User_Id,
Key.From,
su.CompleteName,
Key.Company_Id,
Key.User_Id_Responsible,
Key.DateTime,
Key.Transaction,
Key.Client_Id,
Key.DateTimeGet,
Key.DateTimeDelivered,
Key.Hours,
Key.ResponsibleName,
Key.PersonalId,
User.CompleteName AS NomeUsuarioVenda,
Client.CompleteName
FROM San_Chave
JOIN User
ON Key.User_Id_Responsible = User.User_Id
JOIN User su
ON Key.User_Id = su.User_Id
LEFT OUTER JOIN Client
ON Key.Client_Id = Client.Client_Id
WHERE (Key.Delivered = 0 OR Key.Delivered is NULL)
and Key.From = 14
AND User.Deleted = 0
在我的WHERE子句中,我需要把AND Client.Deleted = 0,但是当Client.CompleteName不为null时。
我该怎么做?
答案 0 :(得分:3)
最后,添加此条件
AND (San_Cliente.NomeCliente is null OR San_Cliente.Excluido = 0)
因此,如果San_Cliente.NomeCliente
为空,则记录传递WHERE子句
当它IS NOT NULL
时,它会检查附加过滤器。
总结一下,
AND San_Cliente.Excluido = 0
答案 1 :(得分:1)
你想:
(San_Chave.NomeCompleto IS NULL or (San_Cliente.NomeCompleto IS NOT NULL AND San_Cliente.Excluido = 0))
可能有一种更简洁的方式来表示这种逻辑情况,但我认为这将是最清楚的。
答案 2 :(得分:1)
使用合并
coalesce(San_Cliente,0) = 0
http://msdn.microsoft.com/en-us/library/ms190349.aspx
SELECT San_Chave.Chave_Id, San_Chave.Usuario_Id, San_Chave.De, su.NomeCompleto,
San_Chave.Credenciada_Id, San_Chave.Usuario_Id_Responsavel, San_Chave.DataHora,
San_Chave.Transacao, San_Chave.Cliente_Id, San_Chave.DataHoraPegou,
San_Chave.DataHoraDevolverPrevisao, San_Chave.DataHoraEntregou,
San_Chave.HorasDevolucao, San_Chave.NomeResponsavel, San_Chave.CpfResponsavel,
San_Chave.RgResponsavel, San_Chave.TelResponsavel, San_Chave.Tel2Responsavel,
San_Chave.Endereco, San_Chave.Devolvido, San_Chave.TextoDevolucao,
San_Usuario.NomeCompleto AS NomeUsuarioVenda, San_Cliente.NomeCompleto
FROM San_Chave JOIN San_Usuario
ON San_Chave.Usuario_Id_Responsavel = San_Usuario.Usuario_Id
JOIN San_Usuario su
ON San_Chave.Usuario_Id = su.Usuario_Id
LEFT OUTER JOIN San_Cliente
ON San_Chave.Cliente_Id = San_Cliente.Cliente_Id
WHERE (San_Chave.Devolvido = 0 OR San_Chave.Devolvido is NULL)
and San_Chave.De = 14
AND San_Usuario.Excluido = 0
AND coalesce(San_Cliente,0) = 0