2015-10-15 50 views
-3

如何修改此查询以获得更好的增强效果?如何更改此查询以获得更好的增强效果?

select * from employee 
where emp_id like '0%' 
    and emp_id not like '00%' 
    or emp_id like '1%' 
    or emp_id = '999'; 

我想与像 '0xxxxxxx', '1xxxxxx',999 emp_id的员工的所有细节,而不是像 '00xxxxxx'。

+0

是什么你的查询有问题吗? –

+0

如果您只有少数员工,则不需要进行性能优化。如果你有一大堆雇员,那么你的查询可能会返回很多很多的行,所以全表扫描不是什么大问题。 –

+1

在担心性能之前,请确保在您的条件下使用括号来获得您想要的结果:'(emp_id像'0%'和emp_id不像'00%')或....' – Lamak

回答

0

您可以使用oracle 10g中引入的regexp_like函数。

你的语句可能看起来像这样到底

select * 
from (select '1xxxxx' d from dual union 
     select '0xxxxx' d from dual union 
     select '00xxxx' d from dual union 
     select '999' d from dual union 
     select '9999' d from dual union 
     select '555' d from dual union 
     select '5xxxxx' d from dual) 
where regexp_like(d, '(^0[^0]|^1)|^999$'); 

|起OR和读取等,与0其次是不0开头或以1开头或等于999

相关问题