[英]sql- create a directory like structure from links
I have a table with column containing many URLs of various files, I want to create a directory like structure, folders and files sort of. 我有一个表,该表的列包含各种文件的许多URL,我想创建一个目录,如结构,文件夹和文件之类的。
Folders first, then files 首先是文件夹,然后是文件
links
----------
http://abcd.com/efgh/hhg.txt
http://abcd.com/efgh/abc/bsbs/hsgs.txt
http://abcd.com/lmn.txt
http://abcd.com/jhksdh/khsdh/khd/abc.txt
http://abcd.com/dsfsdh/khsdh/dsfsdf/bsbs.txt
when entered http://abcd.com/
in query it should produce 在查询中输入
http://abcd.com/
时,应产生
╔════╦═════════════════╗
║ ║ ║
╠════╬═════════════════╣
║ 1 ║ efgh ║
║ 2 ║ jhksdh ║
║ 3 ║ dsfsdh ║
║ 4 ║ lmn.txt ║
╚════╩═════════════════╝
when entered http://abcd.com/efgh/
in query it should produce 在查询中输入
http://abcd.com/efgh/
,它应产生
╔════╦═════════════════╗
║ ║ ║
╠════╬═════════════════╣
║ 1 ║ abc ║
║ 2 ║ hhg.txt ║
╚════╩═════════════════╝
I can't make up a query using SUBSTRING_INDEX() string function 我无法使用SUBSTRING_INDEX()字符串函数来构成查询
Is this close enough to what you want? 这足够接近您想要的吗?
select l.*
from links l
where l.link like concat(@link, '/%')
I ask, because usually such a table would often (properly?) have all levels as separate rows. 我问,因为通常这样的表经常(适当地?)将所有级别作为单独的行。 So,
links
would have rows for: 因此,
links
将包含以下行:
http://abcd.com/efgh/abc/bsbs/hsgs.txt
http://abcd.com/efgh/abc/bsbs
http://abcd.com/efgh/abc
http://abcd.com/efgh
http://abcd.com
http://
Then the following query does what you want: 然后,以下查询执行您想要的操作:
select l.*
from links l
where l.link like concat(@link, '/%') and
l.link not like concat(@link, '/%/%');
Without changing the data, this would generally work: 在不更改数据的情况下,通常可以正常工作:
select concat(@link, '/',
substring_index(substring(@link, length(@link) + 1
),
'/', 1
)
)
from links l
where l.link like concat(@link, '/%');
Try this one 试试这个
set @param = 'http://abcd.com/efgh/';
select
distinct SUBSTRING_INDEX(REPLACE(link, @param, ''), '/', 1) as link
from links
where POSITION(@param IN link) > 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.