本文是用oracle語法用作演示,使用到了instr,substr函數。
instr
like X%
select*from dual where 'asdfg' like 'a%';
select*from dual where instr('asdfg', 'a') = 1;
like %X
select*from dual where 'asdfg' like '%fg';
select*from dual
where instr('asdfg', 'fg') != 0
and instr('asdfg', 'fg') + length('fg') - 1 = length('asdfg');
like %X%
select*from dual where 'asdfg' like '%df%';
select*from dual where instr('asdfg', 'df') > 0;
substr
instr足以應對絕大多數場景,但是某個數據庫偶爾會有無法使用instr的情況出現。因此把substr可以應對的兩種方法列出來。
like X%
select*from dual where 'asd' like 'as%';
select*from dual where substr('asd', 1, length('as')) = 'as';
like %X
select*from dual where 'asd' like '%sd';
select*from dual where substr('asd', length('asd') - length('sd') + 1) = 'sd';
regexp_like
like可以算作regexp_like的子集,所有like語句都可以用regexp_like輕鬆改寫。而regexp_like使用的是正則表達式,只要你會正則表達式,都可以直接使用regexp_like。
正則表達式的語法建議百度,這裏只給出一個簡單例子。
select*from dual where '123456' like '%3%5%';
select*from dual where regexp_like('123456', '*3*5*');