Tuesday, May 18, 2010

Using binding vareable with “LIKE” condition

這感覺就是一個相當簡單又會很常用到的東西... 卻一時寫不出來OTZ

通常我們用到LIKE時,大多就是想做模糊搜尋吧...

像下面這個SQL是要找名字裡包含大寫SM的人:
SELECT * FROM EMPLOYEE WHERE NAME LIKE ‘%SM%’

像上面這樣寫,就變成傳說中的hard coding,有時候為了效能考量,我們要把值的地方以變數(:variable)取代,所以這兩種組合揪竟該怎麼兜在一起呢!?

這樣… LIKE '%:variable%'          (X, :variable直接被當成字串=   =)
這樣… LIKE ''%:variable%''        (X, :variable終於不被當成字串,還是失敗)
這樣… LIKE '%'||:variable||'%'     (O, 原來要自己組....)

Comments

0 Responses to "Using binding vareable with “LIKE” condition"

Post a Comment

Tags