SQL查询对我没有意义

我认为我的where子句是错误的。

我的困境是,如果用户没有tbl_dentalBuyerInsurance中的记录,这意味着他们正在全力以赴。

因此,如果用户在tbl_dentalBuyerInsurance中没有记录,我希望他们回来作为结果。 如果他们在tbl_dentalBuyerInsurance中有记录并且匹配使用LIKE或相等,我也希望他们回来。

SELECT
[dbo].[tbl_users].*, [dbo].[tbl_dentalBuyerInsurance].*
     FROM
[dbo].[tbl_users]
    LEFT OUTER JOIN [dbo].[tbl_dentalBuyerInsurance] ON [dbo].[tbl_dentalBuyerInsurance].buyerId = [dbo].[tbl_users].id
    LEFT OUTER JOIN [dbo].[tbl_dentalInsurance] ON [dbo].[tbl_dentalInsurance].id = [dbo].[tbl_dentalBuyerInsurance].dentalInsuranceId
    WHERE
(
    (
        [dbo].[tbl_dentalInsurance].companyName LIKE '%Cigna%'
        OR [dbo].[tbl_dentalInsurance].companyName = ''
    )
    AND(
        [dbo].[tbl_dentalBuyerInsurance].ppo = 1
        OR [dbo].[tbl_dentalBuyerInsurance].ppo = ''
    )
    AND(
        [dbo].[tbl_dentalBuyerInsurance].hmo = 0
        OR [dbo].[tbl_dentalBuyerInsurance].hmo = ''
    )
)
采纳答案:

鉴于您正在使用LEFT JOINS,如果在连接的“右侧”没有匹配的记录,则所有这些右侧字段将为NULL ,而不是空字符串。 您必须使用.... OR whatever IS NULL显式检查,因为NULL不能等于任何东西,包括它自己。

author: marc-b

参考更多解答: SQL Query Not making Sense to me ,转载请保留出处SQL查询对我没有意义及作者信息

Statement: We respect knowledge and authors. Since the content comes from the Internet and is intended for scientific research, any reprinters should retain the author's signature and origin. If you are the author of the content and feel in dispute, please contact email: 1076545519@qq.com. We will find out the situation and deal with it in time. We sincerely thank the author for his hard work.


更多:sql