'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.
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Francisco Zarabozo |