0

请帮助将这两个语句混合为一个语句:

  1. SELECT * FROM inventory WHEREpro_status ='1' orpro_status='2'

  2. SELECT * FROM inventory WHEREloca_city = 'tw' or loca_city= '美国'`

我需要语句 1 的结果来执行语句 2。

//Property Status -0=suspended;1=valid(NORMALl);2=valid(GOOD);3=no/wrong number.
Data Base

ref pro_type    loca_city   pro_statusRemarks: 
00001   Industrial  HK      0
00002   Industrial  TW      2
00003   Commercial  HK      2
00004   Retail      TW      0
00005   Others      HK      2
00006   Retail      HK      2
00007   Others      HK      2
00008   Retail      USA     2
00009   Retail      HK      1
00010   Retail      HK      1
00011   Retail      TW      2
00012   Retail      HK      2
00013   Retail      HK      2
00014   Retail      HK      2
00015   Retail      HK      2
00016   Commercial  HK      1
00017   Retail      HK      1
00018   Others      HK      1

需要结果:

ref pro_type    loca_city   pro_statusRemarks: 
00002   Industrial  TW      2
00008   Retail      USA     2
00011   Retail      TW      2
4

3 回答 3

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')
于 2013-07-16T07:18:09.647 回答
0
SELECT * FROM inventory 
WHERE `pro_status`='1' 
   OR `pro_status`='2' 
   OR `loca_city` = 'hk' 
   OR `loca_city` = 'tw' 
   OR loca_city` = 'usa'
于 2013-07-16T07:12:45.480 回答
0

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

SELECT 
* 
FROM inventory 
WHERE `pro_status`='1' or `pro_status`='2' 
OR `loca_city` = 'hk' or `loca_city` = 'tw' or `loca_city` = '
于 2013-07-16T07:10:52.857 回答