2012-01-27 10 views
0

私のwhere節が間違っていると思います。SQLクエリが私に感覚しない

私のジレンマは、ユーザーがtbl_dentalBuyerInsuranceのレコードを持っていない場合、つまり、すべてのデータを取得しているということです。

したがって、ユーザーがtbl_dentalBuyerInsuranceにレコードを持っていない場合は、結果として返信してもらいたいです。 tbl_dentalBuyerInsuranceにレコードがあり、それがLIKEまたは同等を使用して一致した場合、彼らに戻ってもらいたい。あなたがLEFTに合流使用しているが与えられ

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

をlenの機能を使用する必要があります。 – Aaron

+0

するだろう:) @ BryceAtNetwork23 –

答えて

3

、参加の「右」側に一致するレコードがありません場合は、それらの右側のすべてのフィールドはNULL、ない空の文字列になります。 .... OR whatever IS NULLでそれを明示的にチェックする必要があります。なぜならNULLはそれ自身を含めて何と同等であってはならないからです。

[dbo].[tbl_dentalInsurance].companyName LIKE '%Cigna%' 
     OR [dbo].[tbl_dentalInsurance].companyName is null 

あなたは空の文字列を許可している場合:それはあなたの最初の間違いだ、あなたはallowinf空の文字列であることを意味し、あなたがnull値を探している場合、クエリがあるので、どのようにMarcBが言っ

+0

これは線量が意味をなす? '\t( \t \t( \t \t \t [DBO]。[tbl_dentalInsurance] .companyName LIKE '%シグナの%' \t \t \t OR [DBO]。[tbl_dentalInsurance] .companyNameは \t \t NULL IS) \t \t AND( \t \t \t [DBO]。[tbl_dentalBuyerInsurance] .ppo = 1 \t \t \t OR [DBO] [DBO]。[tbl_dentalBuyerInsurance] .ppoが \t \t)\t \t NULLであり、( \t \t \t。[tbl_dentalBuyerInsurance] .hmo = 0 \t \t \t OR [DBO]。[tbl_dentalBuyerInsurance]。 hmoはNULLです \t \t) \t) ' –

+0

ありがとうございます@Marc B ...これは参考になりました。 –

1
[dbo].[tbl_dentalInsurance].companyName LIKE '%Cigna%' 
     OR [dbo].[tbl_dentalInsurance].companyName = '' 

あなたはあなたが本当にあなたのテーブルの別名を使用してみてくださいな長さを検証値の0

saludos

+0

MarcBの回答ありがとうございます:) –

関連する問題