SQl语句能在from中做判断吗?

根据条件的不同,查询不同的表,能在from中判断实现吗?
我现在使用的是select * from (select * from table1 where 'a' in ('a','b') union all (select * from table1 where 'a' in ('c','d'))),但是这样做速度很慢,用不上索引,能在from 直接判断吗?
SQL写错了,是select * from (select * from table1 where 'a' in ('a','b') union all (select * from table2 where 'a' in ('c','d'))),

这样写容易读些:

select * from table1 where 字段a in ('a','b')
union all
select * from table2 where 字段a in ('c','d')

需要对两个表的 字段a 建立索引。

判断(条件)基本都是写在 Where 子句中,在有关联(Join)时,On 子句中可以写一部分。

GoodLuck!
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-04-20
用不上索引,那就不用子查询,直接union all可以提高效率。
另外把 * 替换成 字段名也可以提高效率。

eg:

select 字段a,字段b ,...字段x from table1 where 字段a in ('a','b')
union all
select 字段a,字段b ,...字段x from table2 where 字段a in ('c','d')
第2个回答  2010-04-20
根据条件不同查不同的表用存储过程效率很高