简体   繁体   English

一个大于2和3或条件的sql查询(mysql)

[英]A sql query with more than 2 and,3 or conditions(mysql)

I want to query mysql table with below condition 我想以以下条件查询mysql表

         where 
   1.      (type = 'Single Family') 
   2.   and (transaction = 'For sale'  )
   3.  and (price BETWEEN '0' and   '7500') 
   4.   and (beds >= '0' and baths >= '0')
   5.  and (city LIKE 'British columbia%' or neigh LIKE 'British columbia%' or province LIKE 'British columbia%');

The result should satisfy 1 & 2 & 3 & 4 & 5 conditions. 结果应满足1&2&3&4&5条件。

In brief: 简单来说:

A property which type is Single Family and it sholud be For sale and price to be in between 0 and 7500 and it's city or neigh or province like British columbia. 类型为“单身家庭”的房地产,其出售价格为0到7500之间,并且是城市或邻近地区或省份,例如不列颠哥伦比亚省。

For that I written query like below 为此,我写了如下查询

select *  
from   tablename  
where type = 'Single Family' 
    and transaction = 'For sale'  
    and price BETWEEN '0' 
    and   '7500' 
    and beds >= '0' 
    and baths >= '0'
    and city LIKE 'British columbia%' 
    or neigh LIKE 'British columbia%' 
    or province LIKE 'British columbia%';

It giving results that having type as not only 'Single Family' but also remaining like 'Fields','Business'.. 它给出的结果不仅具有“单一家庭”的类型,还具有类似于“字段”,“业务”的类型。

Then I again written the query which returned only type='Single Family' but it failed in other conditions 然后我再次编写了仅返回type ='Single Family'的查询,但在其他情况下失败了

select *  
from   tablename  
where transaction = 'For sale'  
    and price BETWEEN '0' 
    and   '7500' 
    and beds >= '0' 
    and baths >= '0'
    and city LIKE 'British columbia%' 
    or neigh LIKE 'British columbia%' 
    or province LIKE 'British columbia%' 
    and type = 'Single Family';

How to write correct sql query for conditions I specified. 如何为我指定的条件编写正确的sql查询。

select *  
from   tablename  
where transaction = 'For sale'  
    and price BETWEEN '0' 
    and   '7500' 
    and beds >= '0' 
    and baths >= '0'
    and (city LIKE 'British columbia%' 
             or neigh LIKE 'British columbia%' 
             or province LIKE 'British columbia%' 
        )
    and type = 'Single Family';

this will treat the or conditions as 1 clause. 这会将or条件视为1子句。

select *
from   tablename 
where
     transaction = 'For sale'
     and price BETWEEN '0' and '7500'
     and beds >= '0' and baths >= '0'
     and city LIKE 'British columbia%'
     or neigh LIKE 'British columbia%'
     or province LIKE 'British columbia%'
     and type = 'Single Family'

Is the same as: 是相同的:

select *
from   tablename 
where
     (
       transaction = 'For sale'
       and price BETWEEN '0' and '7500'
       and beds >= '0' and baths >= '0'
       and city LIKE 'British columbia%'
     )
     or neigh LIKE 'British columbia%'
     or (
       province LIKE 'British columbia%'
       and type = 'Single Family'
     )

Use brackets to issolate the or's. 使用方括号分隔or。

select *
from   tablename 
where
     transaction = 'For sale'
     and price BETWEEN '0' and '7500'
     and beds >= '0' and baths >= '0'
     and (
       city LIKE 'British columbia%'
       or neigh LIKE 'British columbia%'
       or province LIKE 'British columbia%'
     )
     and type = 'Single Family'

Since and has higher precedence than or , you should surround your or conditions with brackets: 由于and优先级高于or ,因此您应在方括号中加上or条件:

SELECT * 
FROM   tablename  
WHERE  type = 'Single Family' AND 
       transaction = 'For sale'  AND
       price BETWEEN '0' and '7500' AND
       beds >= '0' AND
       baths >= '0' AND 
       (city LIKE 'British columbia%' OR 
        neigh LIKE 'British columbia%' OR 
        province LIKE 'British columbia%') AND
       type = 'Single Family';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM