Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there basename() equivalent in MySQL?

Tags:

path

php

mysql

basename in PHP returns the file name of a path.

Is there a way to do this within the SELECT statement of a MySQL Query? It would be nice to have this so I can apply ORDER BY directly to it.

like image 359
bytecode77 Avatar asked Aug 23 '15 11:08

bytecode77


People also ask

What is Basename function in PHP?

The basename() function returns the filename from a path.

What is Flag in mysql?

mysql flags are used by the compiler and set the enviroment you wish to work in. There's flags such as: '--with-archive-storage-engine' wgich does what it states, it would start mysql process but also start the system in archive able content mode.

What is parameter in mysql?

In general, a parameter is a placeholder for a variable that contains some value of some type when executing a general-purpose query, or arguments and return values when a stored procedure is executed. Parameter is represented by MySql.

What is mysql command?

mysql is a simple SQL shell with input line editing capabilities. It supports interactive and noninteractive use. When used interactively, query results are presented in an ASCII-table format. When used noninteractively (for example, as a filter), the result is presented in tab-separated format.


1 Answers

You can use the SUBSTRING_INDEX function to get the last element after a specific delimiter, in this case a slash. This can also be used with a backslash, simply use "\\" in that case.

SELECT
    Path,
    SUBSTRING_INDEX(Path, '/', -1) AS FileName
FROM
    files

Example result row:

Path = downloads/apps/file1.exe
FileName = file1.exe

like image 87
bytecode77 Avatar answered Oct 03 '22 01:10

bytecode77