我如何 select 行与从相同 table 连接的额外列

How can I select rows with an extra column joined from the same table

我有一个 table 这样的:

id | path      | name | date       | data
---+-----------+------+------------+-----
1  | Docs      | 1000 | 2022-01-01 | aaa0
2  | Docs/1000 | Text | 2022-01-11 | AAA0
3  | Docs      | 1001 | 2022-02-02 | aaa1
4  | Docs/1001 | Text | 2022-02-12 | AAA1

如何 select 路径为 'Docs' 的所有行并添加相应 'Text' 的日期,即:

id | path | name | date       | date_of_text | data
---+------+------+------------+--------------+-----
1  | Docs | 1000 | 2022-01-01 | 2022-01-11   | AAA0
3  | Docs | 1001 | 2022-02-02 | 2022-02-12   | AAA1

你可以通过自连接达到你想要的结果-

SELECT T1.id, T1.path, T1.name, T1.date, T2.date date_of_text, T2.data
  FROM table_name T1
  LEFT JOIN table_name T2 ON T1.name = SUBSTRING(path, POSITION("/" IN path) + 1, LENGTH(path))
 WHERE T1.path = 'Docs'

有很多方法可以做到这一点,包括

相关子查询

select t.*,id % 2 ,(select date from t t1 where t1.ID = t.id + 1) datetext
from t
where id % 2 > 0;

自己加入

select t.*,t.id % 2 , t1.date
from t
join t t1 on t1.ID = t.id + 1
where t.id % 2 > 0;

聚合

select min(id) id,min(path) path,min(date) date,upper(data) data ,max(date) datetext
from t
group by t.data;