Matt Kieran
Matt Kieran

Reputation: 4220

How can you find a variable length substring in MySQL?

I have several strings defined in a php script:

<?php
    $array = [
        'foobar',
        'helloworld',
        'applebanana'
    ];
?>

From those strings I've generated several substrings and put them into a database:

+-------------+---+
| id | substrings |
+----+------------+
| 1  | bar        |
| 2  | world      |
| 3  | banana     |
+----+------------+

I can't say how long each string/substring will be and I cannot use a delimiter. But I can say that the substring is always positioned to the very most right of the original.

So what I'd like to know is, how can I use the longer strings to find the shorter substrings of it in the database?

Note: I can't really use substring() or right() because I don't know the length of the substring in the database.

Upvotes: 0

Views: 225

Answers (1)

Francisco Zarabozo
Francisco Zarabozo

Reputation: 3749

Given any string you have, you can still use right in conjunction with lenght:

select * from substr_table where right('foobar', length(substrings)) = substrings

However, this sounds like a terrible application design.

Upvotes: 2

Related Questions