我有一个 mysql 表,其中有一个名为 orig_file
的字段,我想从该表中获取目录结构。 orig_file 字段包含目录和文件名。我现在使用一个非常复杂的查询(它正在运行),我想知道是否有更好(或更优雅)的解决方案。
SELECT left(orig_file,length(orig_file)+1-instr(reverse(orig_file),"/")) as 'directory' FROM tablename GROUP BY left(orig_file,length(orig_file)+1-instr(reverse(orig_file),"/")) ORDER BY orig_file ASC;
是否有一个函数可以返回一个字符串在另一个字符串中最后一次出现的索引?
length(orig_file)+1-instr(reverse(orig_file),"/")
给出了结果,但是相当复杂...
最佳答案
试试这个
SELECT SUBSTRING('this/is/test/directory/path/filename',1,LENGTH("this/is/test/directory/path/filename") - LOCATE('/', REVERSE("this/is/test/directory/path/filename"))+1);
关于mysql - 从带有文件名的 mysql 表中返回目录结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26156204/