如何从带有文件路径的列中获取MySQL中的左子字符串?显示整个文件路径字符串,不包括文件名?
要获得左子字符串,请使用LEFT()以及substring_index()。例如,假设文件路径为-
“/MyFile/JavaProgram/Hello.java “
在这里,我们将看到如何显示整个文件路径,除了文件名,即-
/MyFile/JavaProgram/
让我们首先创建一个表-
create table DemoTable ( FileLocation text ); insert into DemoTable values('/C/AllPrograms/animation.gif'); insert into DemoTable values('/E/FavFile/ChatProgram.java');
使用select语句显示表中的所有记录-
select *from DemoTable;
这将产生以下输出-
+--------------------------------+ | FileLocation | +--------------------------------+ | /MyFile/JavaProgram/Hello.java | | /C/AllPrograms/animation.gif | | /E/FavFile/ChatProgram.java | +--------------------------------+ 3 rows in set (0.00 sec)
以下是在MySQL中获取左子字符串的查询-
select left(FileLocation,char_length(FileLocation)-char_length(substring_index(FileLocation,'/',-1))) from DemoTable;
这将产生以下输出-
+------------------------------------------------------------------------------------------------+ | left(FileLocation,char_length(FileLocation)-char_length(substring_index(FileLocation,'/',-1))) | +------------------------------------------------------------------------------------------------+ | /MyFile/JavaProgram/ | | /C/AllPrograms/ | | /E/FavFile/ | +------------------------------------------------------------------------------------------------+ 3 rows in set (0.00 sec)