我有 2 个交叉引用表,称为“场景”和“文件”:
场景
id created_at updated_at scene_id
-- ----------- ---------- ----------
1 2024-02-13 2024-03-05 vt-1105
2 2024-02-14 2024-03-06 KA-338
3 2024-02-15 2024-03-07 JU-182
4 2024-02-16 2024-03-08 PX-118
5 2024-02-17 2024-03-09 po-6339085
6 2024-02-18 2024-03-10 la-426
文件
id created_at updated_at filename scene_id
-- ----------- ---------- ------------------- --------
1 2024-03-03 2024-03-06 KA-338-A_180_LR.mp4 2
2 2024-03-03 2024-03-06 KA-338-B_180_LR.mp4 2
3 2024-03-03 2024-03-06 KA-338-C_180_LR.mp4 2
4 2024-03-04 2024-03-07 JU-182-A_180_LR.mp4 3
5 2024-03-04 2024-03-07 JU-182-B_180_LR.mp4 3
6 2024-03-04 2024-03-07 JU-182-C_180_LR.mp4 3
7 2024-03-04 2024-03-07 JU-182-D_180_LR.mp4 3
8 2024-03-05 2024-03-07 PX-118-A_180_LR.mp4 4
9 2024-03-05 2024-03-07 PX-118-B_180_LR.mp4 4
我想拍摄场景“JU-182”(例如),该场景有 4 个链接到它的视频文件(“文件”表中的“scene_id”= 3),并创建链接到每个单独的 4 个新的重复场景视频。我正在尝试在纯 SQLite 中执行此操作。
所以我会向“场景”表中添加 4 个重复的行...
场景
id created_at updated_at scene_id
-- ----------- ---------- ----------
1 2024-02-13 2024-03-05 vt-1105
2 2024-02-14 2024-03-06 KA-338
3 2024-02-15 2024-03-07 JU-182
4 2024-02-16 2024-03-08 PX-118
5 2024-02-17 2024-03-09 po-6339085
6 2024-02-18 2024-03-10 la-426
7 2024-02-15 2024-03-07 JU-182
8 2024-02-15 2024-03-07 JU-182
9 2024-02-15 2024-03-07 JU-182
10 2024-02-15 2024-03-07 JU-182
...并更新“files”表,以便“scene_id”为“3”的所有行(第 4、5、6、7 行)更改为 7、8、9、10(新创建的文件的 ID)场景):
文件
id created_at updated_at filename scene_id
-- ----------- ---------- ------------------- --------
1 2024-03-03 2024-03-06 KA-338-A_180_LR.mp4 2
2 2024-03-03 2024-03-06 KA-338-B_180_LR.mp4 2
3 2024-03-03 2024-03-06 KA-338-C_180_LR.mp4 2
4 2024-03-04 2024-03-07 JU-182-A_180_LR.mp4 7
5 2024-03-04 2024-03-07 JU-182-B_180_LR.mp4 8
6 2024-03-04 2024-03-07 JU-182-C_180_LR.mp4 9
7 2024-03-04 2024-03-07 JU-182-D_180_LR.mp4 10
8 2024-03-05 2024-03-07 PX-118-A_180_LR.mp4 4
9 2024-03-05 2024-03-07 PX-118-B_180_LR.mp4 4
我尝试仅使用场景 ID 来实现此目的,因此我将值存储在名为“变量”的临时表中,如下所示:
CREATE TEMP TABLE variables (
scene_id TEXT,
scene_index INT,
files_total INT,
file_ids TEXT
);
INSERT INTO variables VALUES (
'JUVR-182', -- manually change to internal scene ID
NULL,
NULL,
NULL
);
UPDATE variables
SET scene_index = (
SELECT id FROM scenes WHERE scene_id = (
SELECT scene_id FROM variables
)
);
UPDATE variables
SET files_total = (
SELECT COUNT(*) FROM files WHERE scene_id = (
SELECT scene_index FROM variables
)
);
UPDATE variables
SET file_ids = (
SELECT GROUP_CONCAT(id, ',') FROM files WHERE scene_id = (
SELECT scene_index FROM variables
)
);
变量
scene_id scene_index scene_newIDs files_total file_ids
-------- ----------- ------------ ----------- --------
JU-182 3 null 4 4,5,6,7
..然后以这种方式复制行:
WITH cte(x) AS (
SELECT 1
UNION ALL
SELECT x + 1
FROM cte WHERE x < (SELECT files_total FROM variables)
)
INSERT INTO scenes (created_at, updated_at, scene_id)
SELECT created_at, updated_at, scene_id
FROM scenes JOIN cte
WHERE id = (SELECT scene_index FROM variables);
...我有点卡在这里。我首先将新场景 ID 存储在变量表中:
变量
scene_id scene_index scene_newIDs files_total file_ids
-------- ----------- ------------ ----------- --------
JU-182 3 7,8,9,10 4 4,5,6,7
不确定我是否以正确的方式处理此问题,但是如何使“文件”表上的第 4,5,6,7 行更新为值 7,8,9,10?
参见示例。完成任务的方法与您的有些不同。
insert into scenes (created_at,updated_at,scene_id)
select created_at,updated_at,scene_code
from(
select *,s.scene_id as scene_code
,count(*)over(partition by s.scene_id) cnt
from files f
left join scenes s on s.id=f.scene_id
) x
where cnt>1
2.将表文件中的 scene_id 更新为新的 id
update files
set scene_id=new_scene_id
from (
select file_id, created_at,updated_at,scene_code ,scene_id,rn,old_scene_id,new_scene_id,srn,frn
from(
select *,s.scene_id as scene_code,f.scene_id old_scene_id,s.id new_scene_id,f.id file_id
,row_number()over(partition by s.scene_id order by f.id) rn
from (select f0.*,s0.scene_id main_scene
,row_number()over(partition by f0.scene_id order by f0.id) frn
from files f0 left join scenes s0 on f0.scene_id=s0.id
) f
left join (select *
,row_number()over(partition by s1.scene_id order by s1.id) srn
from scenes s1
) s on s.scene_id=f.main_scene and (s.srn-1)=f.frn
) x
)y
where y.file_id=files.id