Skip to content
All writing Part 05 of 09 · 一個操作,三種引擎
Engineering · 1 min read

LIKE vs ILIKE:跨引擎的子字串搜尋,順帶談大小寫

SQL 的子字串搜尋:LIKE 與 ILIKE 怎麼運作、% 與 _ 萬用字元、跳脫,以及同一個操作如何對應到 MongoDB 的 $regex($options: 'i')。

「在一段字串裡找子字串」大概是最常做的 operation 之一。SQL 引擎靠 LIKE / ILIKE 配上 pattern 來處理。

string LIKE/ILIKE pattern

  • 回傳「string 是否符合 LIKE 的 pattern」
    • string LIKE patternstring NOT ILIKE pattern
    • 差別:ILIKE不分大小寫(case-insensitive)。

pattern 規則

  • %:任意數量的任意字元(含零個)。
  • _:單一個任意字元。
  • \:跳脫字元,用來比對字面上的 %_\
  • 例:%minor_action_hr%

換到 Mongo 這邊,同一件事是用 $regex 做($options: "i" 就等於 ILIKE)→ MongoDB 運算子與詞彙對照


參考來源

Related: 回到跨引擎資料庫查詢總覽,或在可攜式 SQL 核心裡看它的定位。

Tags #sql #databases
// connect

Be brave | Be wise | Be grateful

21 BreakinCode

// elsewhere
LinkedInMedium (lang: en)Youtube
wh:~$William Hung· © 2026 Taipei · GMT+8 · Available for collaboration