2012-06-08 40 views
0

可能重複:
Is there a combination of 「LIKE」 and 「IN」 in SQL?怎麼樣,並在查詢中使用

,我們可以像使用,並在相同的查詢條件類似這樣的

select inticap(last_name),length(last_name) from employees 
    where last_name in(like '%J%',like '%A%',like '%M%'); 

OR 

    select inticap(last_name),length(last_name) from employees 
where last_name like in('%J%', '%A%',l'%M%'); 

我知道這不會工作。在這裏,我想顯示姓氏首字母和姓氏的長度,我想顯示帶有字母'A','J'和'M'的名字。請幫我

+0

http://stackoverflow.com/questions/4335359/is-it-possible-to-use-like-and-in-for-a-where-statment和http://stackoverflow.com/questions/10747996/in-sql-server-how-do-i-search-for-multiple-values-in-a-where-i-need-to-u/10748091#10748091和許多其他人 –

+0

不是贏得'您可以在像last_name這樣的查詢中使用OR,如'%J%'或者像'%A%' – shobhit

+0

這是不可能的。 –

回答

3

正如已經提到的,你不能直接結合LIKEIN,你會更喜歡。

這裏有一個缺憾選項:

SELECT INITCAP(last_name) 
,  LENGTH(last_name) 
FROM employees 
WHERE +1 IN (SIGN(INSTR(last_name,'J')) 
      , SIGN(INSTR(last_name,'A')) 
      , SIGN(INSTR(last_name,'M'))) 

或者,如果你是至少10g中,可以使用正則表達式:

SELECT INITCAP(last_name) 
,  LENGTH(last_name) 
FROM employees 
WHERE REGEXP_LIKE(last_name,'(J|A|M)') 

...但在一般的正則表達式更比你花園式的SQL函數更佔用CPU資源。

3
select inticap(last_name),length(last_name) from employees 
where last_name like '%J%' OR last_name like '%A%' OR last_name '%M%'; 
+0

嗨,我知道它會工作,但我想寫入查詢和像 –

+0

你可以看看 - http://stackoverflow.com/questions/3350106/how-to-use-like-clause-with-in-caluse- in-sql-server –

+1

關於之前評論中的鏈接:不是創建一個表格,你也可以使用piplined函數,並以編程方式將你喜歡的條件添加到PL/SQL表格中。 – Dzik

相關問題