sql sqlite 分割使用逗号分隔的字段

创建日期: 2022-11-08 19:22 | 作者: 风波 | 浏览次数: 21 | 分类: SQL

例如字段为:

Category
------------------------------
Auto,A,1234444
Auto,B,2345444
Electronincs,Computer,33443434

sql 代码如下:

WITH split(word, csv) AS (
  -- 'initial query' (see SQLite docs linked above https://www.sqlite.org/lang_with.html)
  SELECT 
    -- in final WHERE, we filter raw csv (1st row) and terminal ',' (last row)
    '', 
    -- here you can SELECT FROM e.g. another table: col_name||',' FROM X
    'Auto,A,1234444'||',' -- terminate with ',' indicating csv ending
  -- 'recursive query'
  UNION ALL SELECT
    substr(csv, 0, instr(csv, ',')), -- each word contains text up to next ','
    substr(csv, instr(csv, ',') + 1) -- next recursion parses csv after this ','
  FROM split -- recurse
  WHERE csv != '' -- break recursion once no more csv words exist
) SELECT word FROM split 
WHERE word!=''; -- filter out 1st/last rows

执行结果如下:

Auto
A
1234444

没看懂!!!

来源:https://stackoverflow.com/questions/24258878/how-to-split-comma-separated-value-in-sqlite

21 浏览
17 爬虫
0 评论