有没有一种方法可以用AND子句来捷径SQL OR子句?

时间:2019-11-14 07:48:17

标签: sql

有没有一种方法可以缩短下面的SQL语句:

SELECT id, name FROM table_name
WHERE
status = 0 && price BETWEEN 20 AND 1000
&& city_id IN (7,8)
OR
status = 0 && price BETWEEN 20 AND 1000
&& country_id IN (4,14)

3 个答案:

答案 0 :(得分:2)

请尝试:

SELECT id, name FROM table_name
WHERE
status = 0 && price BETWEEN 20 AND 1000
&& (city_id IN (7,8) OR country_id IN (4,14))

答案 1 :(得分:1)

您可以在下面尝试-

SELECT id, name FROM table_name
WHERE status = 0 and price BETWEEN 20 AND 1000 and 
(city_id IN (7,8,) or country_id IN (4,14))

答案 2 :(得分:1)

这是您的查询。

select id, name from table_name
where
    status = 0 and price between 20 and 1000
and (
        city_id IN (7,8)
        or  
        country_id IN (4,14)
    )