How to split comma-separated value in SQLite?

前端 未结 5 538

I want to split comma-separated String inside SQLite database

Example: I have a Category column in 1 of my table.

|Category         


        
5条回答
  •  隐瞒了意图╮
    2020-12-01 17:05

    I like the answer from @user1461607 except: it seems to me the SQLite documentation warns against assuming any particular order from a SELECT, both in the general case, and in the specific case of a recursive SELECT. Here, I modified that answer to add an ordering column in a manner that I think SQLite guarantees to work.

    I also cosmetically changed the example from a comma-separated list to a path, to suggest there are cases where you really need to process things in a particular order. This example also prints out all the columns from the temporary table so it's slightly easier to see what went on. AFAICT, a CTE in SQLite does not have the usual ROWID column, so it seems like adding some ordering column yourself really is required to sleep soundly at night.

    WITH RECURSIVE split(seq, word, str) AS (
        SELECT 0, '/', 'home/ronburk/layers/branch'||'/'
        UNION ALL SELECT
            seq+1,
            substr(str, 0, instr(str, '/')),
            substr(str, instr(str, '/')+1)
        FROM split WHERE str != ''
    ) SELECT * FROM split ORDER BY split.seq ASC;
    

提交回复
热议问题