如何从带有文件路径的列中获取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)
热门推荐
10 祝妹妹毕业祝福语简短
11 新年祝福语短句简短搞笑
12 鼓掌祝福语简短10字
13 浪漫的生日祝福语简短
14 带清的简短祝福语
15 最搞笑生日祝福语简短
16 早安祝福语大全简短励志
17 简短56岁生日祝福语
18 生日祝福语大全女孩简短