【发布时间】:2020-02-25 05:58:32
【问题描述】:
我有两张表,一张是sections,这有一个id,一个parent_table和一个parent_id,另一个是pages,它有一个id和status(以及其他) 列。一个节的父级可以是另一个节或页面,具有相应的parent_table 值。如何使用窗口函数找出给定部分是否在 status = 1 页面上? (使用 MySQL 8.0(或 MariaDB 10.2),但在必要时我可以转换为 postgresql)。
示例数据:
部分
id|parent_table|parent_id
1 |pages |1
2 |sections |1
3 |pages |2
4 |sections |2
页面
id|status
1|1
2|0
要重新创建示例数据:
create table pages (id int not null primary key, status int not null) collate utf8mb4_general_ci;
create table sections (id int not null primary key, parent_table varchar(20), parent_id int not null) collate utf8mb4_general_ci;
insert into pages values (1,1),(2,0);
insert into sections values (1,'pages',1),(2,'sections',1),(3,'pages',2),(4,'sections',2);
【问题讨论】:
标签: mysql window-functions mysql-8.0