1

我要查询数据库中的数据,有下表

COLUMNS:  Type, Location, name, etc.

DATA:
 1. Stores in NJ, name = xyz
 2. Restaurants in NY
 3. Hotels in US
 4. Stores in PA
 5. Restaurants in VA
 6. Hotels in MD
 7. Stores in NJ, name = abc
 8. etc.

我需要一个查询,从 1、2、3 中获取数据

现在,我有以下查询。这运行得更快。但是没有UNION我可以使用任何其他查询吗

select type, location from table1 
where type='stores' and location='NJ' and name='XYZ'
  UNION
select type, location from table1 
where type='restaurants' and location='NY'
  UNION
select type, location from table1 
where type='hotels' and location='US'
4

2 回答 2

2

您可以使用或:

select type, location from table1 
where (type='stores' and location='NJ' and name='XYZ')
or (type='restaurants' and location='NY')
or (type='hotels' and location='US');
于 2012-06-06T16:07:24.830 回答
1

UNION ALL比使用更快,OR并且 prolly 是您真正需要的:

select type, location from table1 
where type='stores' and location='NJ' and name='XYZ'
  UNION ALL
select type, location from table1 
where type='restaurants' and location='NY'
  UNION ALL
select type, location from table1 
where type='hotels' and location='US'

如果你真的想要OR

select type, location from table1 
where (type='stores' and location='NJ' and name='XYZ')
or (type='restaurants' and location='NY')
or (type='hotels' and location='US');

为了使您的查询更快,请在typelocation列上创建索引name

于 2012-06-06T16:10:15.597 回答