1

我有两条指令,我需要一条 SQL 指令在 2 列中返回。

Column 1: Subfolders.
Column 2: Main folders of this subfolders.

--主文件夹

select distinct folder_path as 'main_folder' 
from X
where folder_path like '%\' order by folder_path

--返回6行

--子文件夹

select distinct folder_path as 'sub_folder'
from X
where folder_path not like '%\' order by folder_path

--返回14行

4

1 回答 1

2

听起来你基本上想要一个JOIN. 假设您的主外观像folder1\, folder2\,而您的子外观像folder1\a, folder1\b

SELECT DISTINCT
    main.folder_path AS main_folder,
    sub.folder_path AS sub_folder
FROM
    X main
    JOIN X sub
        ON main.folder_path = LEFT(sub.folder_path, LEN(main.folder_path))
于 2013-06-05T16:59:04.667 回答