2016-09-06 4 views
0

'inner join'を持つ疑わしいSQLクエリがあります。結果は私が期待しているように来ていません。さらに、私は '内部結合'を取り除きます - 結果にはすべてのものも含まれています - 内部結合のように。内部結合を使用するとSQL結果が期待どおりにならない

私はこのような条件から結果が欲しいと思ったらどうしますか?

  1. prop_type = '5'
  2. ない prop_price = '0'
  3. prop_price2 = '0'
  4. prop_price3 = '0'

のMysql:

select prop_lang, prop_type, prop_for, 
ppix_cover,prop_price,prop_price2,prop_price3 
from prop_db 
inner join prop_pix on prop_db.prop_sid=prop_pix.prop_sid 
where prop_db.prop_type='5' 
and prop_db.prop_lang='1' 
and prop_pix.ppix_cover='1' 
and prop_db.prop_for='1' or prop_db.prop_for='0' 
and prop_price3='0' 
and prop_price2='0' 
and not prop_price='0' 
group by prop_db.prop_sid 
order by prop_db.prop_id 
LIMIT 0, 9 

これは上記のクエリの結果です。

enter image description here

ご覧のとおり、列「prop_typeは、」私は期待していない他の値を思い付きます。また、最初の行は、の価格2の価格3で、 '0'ではありません。

結果を上記の条件として表示するにはどうすればよいですか?

+0

あなたのORステートメントが原因です。私はあなたのテーブルのサンプルを持っていませんが、ORステートメントは(ここでは)OR(ここではどこか)として読み込みます。したがって、クエリには両方の条件に一致するすべてが含まれます。かっこを使用してロジックをクリアにし、これが起こらないようにします。 –

答えて

1

野生の推測ですが、通常は次の場所にあります。andおよびorにはカッコが必要です。

ハーフウェイダウン

select prop_lang, prop_type, prop_for, 
ppix_cover,prop_price,prop_price2,prop_price3 
from prop_db 
inner join prop_pix on prop_db.prop_sid=prop_pix.prop_sid 
where prop_db.prop_type='5' 
and prop_db.prop_lang='1' 
and prop_pix.ppix_cover='1' 
and (prop_db.prop_for='1' or prop_db.prop_for='0') 
and prop_price3='0' 
and prop_price2='0' 
and not prop_price='0' 
group by prop_db.prop_sid 
order by prop_db.prop_id 
LIMIT 0, 9 
1

ジャストブラケットの内側に

をあなたの 'または' ステートメントを置く
prop_db.prop_for='1' or prop_db.prop_for='0' 

select prop_lang, prop_type, prop_for, 
    ppix_cover,prop_price,prop_price2,prop_price3 
    from prop_db 
    inner join prop_pix on prop_db.prop_sid=prop_pix.prop_sid 
    where prop_db.prop_type='5' 
    and prop_db.prop_lang='1' 
    and prop_pix.ppix_cover='1' 
    and (prop_db.prop_for='1' or prop_db.prop_for='0') 
    and prop_price3='0' 
    and prop_price2='0' 
    and not prop_price='0' 
    group by prop_db.prop_sid 
    order by prop_db.prop_id 
    LIMIT 0, 9 
1

が同様にこれを試してみてください変更され、これを試してみてください:

SELECT prop_lang, 
    prop_type, 
    prop_for, 
    ppix_cover, 
    prop_price, 
    prop_price2, 
    prop_price3 
FROM prop_db 
INNER JOIN prop_pix ON prop_db.prop_sid = prop_pix.prop_sid 
WHERE prop_db.prop_type='5' 
    AND prop_db.prop_lang='1' 
    AND prop_pix.ppix_cover='1' 
    AND prop_db.prop_for IN ('1','0') 
    AND prop_price3='0' 
    AND prop_price2='0' 
    AND prop_price <> '0' 
GROUP BY prop_db.prop_sid 
ORDER BY prop_db.prop_id 
LIMIT 0, 9 
関連する問題