2014-10-02 70 views
1

我有一個名爲orig_file的字段的mysql表,並希望從該表中獲取目錄結構。 orig_file字段包含目錄和文件名。我現在使用一個相當複雜的查詢(這是工作),我想知道是否有更好的(或更優雅的)解決方案。從帶有文件名的mysql表中返回目錄結構

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),"/") 

給出結果,但相當複雜...

+0

你應該接受的答案,如果這是正確的:) – 2014-10-16 06:19:19

回答

1

試試這個

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); 
+0

看到我的最新答案。 – 2014-10-02 12:47:57

+0

不知道哪種方式更好:)但在這裏,我只是給你的方式替代:P – 2014-10-02 12:49:40

相關問題