2012-09-26 69 views
1
I have the following query: 

Select 
Count(Distinct Case When Play.Uuid Like ('i~%') Then Tap.Player_Id End) As Tapjoy_Ios 
From 
Player_Tapjoy Tap 
Inner Join 
Player Play 
On 
Tap.Player_Id = Play.Player_Id 
Where 
Trunc(Tap.Create_Dtime) >= To_Date('2012-Jan-01','yyyy-mon-dd') 

我想添加另一個像約束,以便結果出來play.uuid like('i〜%'或'ti〜%' )..但這似乎並不奏效。我怎麼能實現這個?SQL Oracle - 想要在案件中添加另一個「like」條件

回答

1

需要兩個全LIKE條款由邏輯OR連接時,LIKE關鍵字(在右側上的左側,字符串值列)的每一個具有兩個左,右兩側。

count(Distinct Case When (Play.Uuid Like 'i~%') OR (Play.Uuid LIKE 'ti~%') Then Tap.Player_Id End) As Tapjoy_Ios 

你也可以做到這一點與單一REGEXP_LIKE,使用常規expressiong ^t?i~.+

count(Distinct Case When REGEXP_LIKE(Play.Uuid, '^t?i~.+') Then Tap.Player_Id End) As Tapjoy_Ios 
  • ^是字符串
  • t?的開始是一個可選t
  • i~是文字
  • .+是任何剩餘的字符,相當於%中的一個常規LIKE
1

你可以只使用一個OR

Select Count(Distinct Case When Play.Uuid Like ('i~%') 
          OR Play.Uuid Like ('ti~%') 
         Then Tap.Player_Id End) As Tapjoy_Ios 
From Player_Tapjoy Tap 
Inner Join Player Play 
    On Tap.Player_Id = Play.Player_Id 
Where Trunc(Tap.Create_Dtime) >= To_Date('2012-Jan-01','yyyy-mon-dd') 
1

雖然答案是正確的,你在錯誤的一級應用的條件。您通常要過濾數據以獲得更好的性能,儘管這裏的LIKE子句使其不太重要。

Select Count(Distinct Tap.Player_Id) As Tapjoy_Ios 
     From Player_Tapjoy Tap 
Inner Join Player Play On Tap.Player_Id = Play.Player_Id 
    Where Tap.Create_Dtime >= To_Date('2012-Jan-01','yyyy-mon-dd') 
     And (Play.Uuid Like ('i~%') OR Play.Uuid Like ('ti~%')) 

正如問題中所寫的那樣,它正在處理所有行,並且對與LIKE模式不匹配的行進行fizzing處理。您也不希望對列運行函數,這不允許使用索引 - 我已更新日期過濾器。你真的不需要TRUNC。