mysql - 混合两个语句

时间:2013-07-16 07:08:39

标签: mysql select

请帮助将这两个陈述混合到一个陈述中:

  1. SELECT * FROM inventory WHERE {pro_status {1}} {pro_status {1}}

  2. ='1' or loca_city ='2' ='usa'`

  3. 我需要声明1的结果来做声明2.

    SELECT * FROM inventory WHERE

    需要结果:

    = 'tw' or loca_city

3 个答案:

答案 0 :(得分:3)

您需要将相应的过滤器与AND结合使用(注意在它们周围加上括号以强制执行所需的优先级):

SELECT *
FROM   inventory
WHERE  (`pro_status`='1' or `pro_status`='2')
   AND (`loca_city` = 'hk' or `loca_city` = 'tw' or loca_city` = 'usa')

使用MySQL IN()运算符可以简化条件:

SELECT *
FROM   inventory
WHERE  pro_status IN (1,2)
   AND loca_city IN ('hk','tw','usa')

答案 1 :(得分:0)

如果两个查询都在同一个表上且所有条件都是OR,您可以直接执行:

SELECT 
* 
FROM inventory 
WHERE `pro_status`='1' or `pro_status`='2' 
OR `loca_city` = 'hk' or `loca_city` = 'tw' or `loca_city` = '

答案 2 :(得分:0)

SELECT * FROM inventory 
WHERE `pro_status`='1' 
   OR `pro_status`='2' 
   OR `loca_city` = 'hk' 
   OR `loca_city` = 'tw' 
   OR loca_city` = 'usa'