」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 使用CTE高效拆分SQLite中的逗號分隔字符串

使用CTE高效拆分SQLite中的逗號分隔字符串

發佈於2025-04-18
瀏覽:356

How to Efficiently Split Comma-Separated Strings in SQLite Using CTEs?

Splitting Comma-Separated Values with SQLite's Common Table Expressions

Question:

How can I effortlessly split a comma-separated string in the Category column of a SQLite table? I seek a simpler approach than using Replace() and Trim() and avoid the limitations of substr().

Answer:

SQLite offers a feature called Common Table Expressions (CTEs) that allows for recursive queries, making it convenient to split comma-separated values.這是一個分解:

說明: ,用單個行包含一個空字符串和comma分離的字符串終止使用一個comma comma comma comma,'[umpur comma''

[[umpur)']查詢':
WITH split(word, csv) AS (
  SELECT '', 'Auto,A,1234444'||','
  UNION ALL
  SELECT substr(csv, 0, instr(csv, ',')), substr(csv, instr(csv, ',')   1)
  FROM split
  WHERE csv != ''
)
SELECT word FROM split
WHERE word!='';
從初始的空行和終端逗號過濾出來結果。

最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3