Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL selecting text after last slash

Tags:

mysql

I have a MySQL column that holds a VARCHAR that looks like this:

folder1/folder2/<Entity>

For example:

folder1/folder2/Apple
folder1/folder2/Microsoft

Because I would like to perform a search on the entity field, I would like to add a column that just contains entity (and the query for $searchTerm%). How can I select that last part after the last / directly in MySQL?

I would like that column to just hold Apple and Microsoft from the example.

like image 708
Noam Avatar asked Aug 10 '14 11:08

Noam


People also ask

How do I select a part of a string in MySQL?

SUBSTRING() function in MySQL function in MySQL is used to derive substring from any given string . It extracts a string with a specified length, starting from a given location in an input string. The purpose of substring is to return a specific portion of the string.

What is `` in MySQL?

People uses `` to surround field names and use '' to surround values. Is that true? Or the major reason to do so is because then we can put the code in a text file and import into the database. thank you. mysql.

How do I fetch the last row?

We can use the ORDER BY statement and LIMT clause to extract the last data. The basic idea is to sort the sort the table in descending order and then we will limit the number of rows to 1. In this way, we will get the output as the last row of the table.


1 Answers

The easiest way is to use substring_index():

select substring_index(mysql_column, '/', -1)
like image 95
Gordon Linoff Avatar answered Sep 28 '22 00:09

Gordon Linoff