SQLite:使用双射表替换结果值

问题描述 投票:0回答:1

在SQLite中,我有以下查询

SELECT x.nameIndex, y.nameIndex FROM relation x, relation y WHERE x.label=y.label;

将返回所有具有相同标签的x.nameIndex,y.nameIndex对。

现在我还有另一个表index2name,我在其中存储每个索引的名称,在这里我可以这样做:

SELECT name FROM index2name WHERE nameIndex=...;

如何更改顶部查询,以便它查询各个索引的名称并改为返回成对的名称?

sqlite join mapping resultset self-join
1个回答
0
投票

使用CTE返回名称,而不是CTE中每一行的索引和组(通过与relation的连接),并对其进行自我连接:

index2name

或不带WITH cte AS ( SELECT i.name, r.`group` FROM relation r INNER JOIN index2name i ON i.nameIndex = r.nameIndex ) SELECT c1.name, c2.name FROM cte c1 INNER JOIN cte c2 ON c2.`group` = c1.`group`;

CTE
© www.soinside.com 2019 - 2024. All rights reserved.