我想在电影Genren中计算一个SQLITE数据库。
在数据库中以保存流派为例,以下是3个示例:
Genres
---------
Action
Western|Crime
Drama|Western|Comedy|Horror
有人对我有解决方案吗?
答案 0 :(得分:0)
问题仍然太模糊,但是为了纪念Stack Overflow上的“变得好”和“回答任何问题”,下面是一个解决方案的开始:
/*
DROP TABLE IF EXISTS movie;
CREATE TEMP TABLE movie
(id INTEGER NOT NULL UNIQUE PRIMARY KEY AUTOINCREMENT,
genres TEXT);
INSERT INTO movie (genres)
VALUES
('Action'),
('Western|Crime'),
('Drama|Western|Comedy|Horror');
*/
WITH RECURSIVE split AS (
SELECT id,
0 AS num,
instr(genres,'|') AS delimit,
null AS genre,
genres AS remain
FROM movie
WHERE genres is not null
UNION
SELECT
id,
num + 1 AS num,
CASE WHEN delimit <= 0 THEN -1
ELSE instr(substr(remain, delimit + 1),'|') END AS delimit,
CASE WHEN delimit <= 0 THEN remain
ELSE substr(remain, 1, delimit -1) END AS genre,
CASE WHEN delimit <= 0 THEN null
ELSE substr(remain, delimit + 1) END AS remain
FROM split
WHERE delimit >= 0
)
SELECT id, num, genre
FROM split
WHERE genre is not null
ORDER BY id, num;