Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL查询对我来说没有意义_Sql_Sql Server_Sql Server 2008_Tsql - Fatal编程技术网

SQL查询对我来说没有意义

SQL查询对我来说没有意义,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,我认为我的where子句是错误的 我的困境是,如果用户在tbl_dentalBuyerInsurance中没有记录,那就意味着他们拿走了所有记录 所以,如果用户在tbl_dentalBuyerInsurance中没有记录,我希望他们返回。 我还希望他们回来,如果他们在tbl_dentalBuyerInsurance中有记录,并且它使用LIKE或equal匹配 SELECT [dbo].[tbl_users].*, [dbo].[tbl_dentalBuyerInsurance].* F

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

我的困境是,如果用户在tbl_dentalBuyerInsurance中没有记录,那就意味着他们拿走了所有记录

所以,如果用户在tbl_dentalBuyerInsurance中没有记录,我希望他们返回。 我还希望他们回来,如果他们在tbl_dentalBuyerInsurance中有记录,并且它使用LIKE或equal匹配

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 = ''
    )
)

如果您使用的是左连接,那么如果连接的“右侧”没有匹配的记录,那么所有这些右侧字段都将是
NULL
,而不是空字符串。您必须使用
显式检查这一点。。。。或者不管什么是NULL,因为NULL永远不能等于任何东西,包括它本身。

如果使用左连接,如果连接的“右侧”没有匹配的记录,那么所有这些右侧字段都将是
NULL
,而不是空字符串。您必须使用
显式检查这一点。。。。或者任何空值
,因为空值永远不能等于任何值,包括它本身

[dbo].[tbl_dentalInsurance].companyName LIKE '%Cigna%'
        OR [dbo].[tbl_dentalInsurance].companyName = ''
这意味着您分配了空字符串,这是您的第一个错误,如果您要查找空值,则如何表示MarcB,因此查询为:

[dbo].[tbl_dentalInsurance].companyName LIKE '%Cigna%'
        OR [dbo].[tbl_dentalInsurance].companyName is null
如果允许使用空字符串,则必须使用len函数验证长度为0的值

萨卢多斯

这意味着您分配了空字符串,这是您的第一个错误,如果您要查找空值,则如何表示MarcB,因此查询为:

[dbo].[tbl_dentalInsurance].companyName LIKE '%Cigna%'
        OR [dbo].[tbl_dentalInsurance].companyName is null
如果允许使用空字符串,则必须使用len函数验证长度为0的值


saludos

你真的应该为你的表使用别名。你真的应该为你的表使用别名。那么这有意义吗<代码>其中([dbo].[tbl\[U dentalBuyerInsurance].[dbo].[Cigna%]或[dbo].[tbl\[U dentalBuyerInsurance].[dbo].[tbl\[U dentalBuyerInsurance].ppo=1或[dbo].[tbl\[U dentalBuyerInsurance].ppo为空)和([dbo].[tbl\[U dentalBuyerInsurance].hmo=0或[dbo].[tbl\[tbl\[U dentalBuyerInsurance].hmo]为空))
谢谢@Marc B。。。这很有帮助。那么这有意义吗<代码>其中([dbo].[tbl\[U dentalBuyerInsurance].[dbo].[Cigna%]或[dbo].[tbl\[U dentalBuyerInsurance].[dbo].[tbl\[U dentalBuyerInsurance].ppo=1或[dbo].[tbl\[U dentalBuyerInsurance].ppo为空)和([dbo].[tbl\[U dentalBuyerInsurance].hmo=0或[dbo].[tbl\[tbl\[U dentalBuyerInsurance].hmo]为空))谢谢@Marc B。。。这很有帮助。