I have a table “locales” with a column named “name”. The records in name always begin with a number of characters folowed by an underscore (ie “foo_”, “bar_”…). The record can have more then one underscore and the pattern before the underscore may be repeated (ie “foo_bar_”, “foo_foo_”).
How, with a simple query, can I get rid of everything before the first underscore including the first underscore itself?
I know how to do this in PHP, but I cannot understand how to do it in MySQL.
SELECT LOCATE('_', 'foo_bar_') ...will give you the location of the first underscore andSUBSTR('foo_bar_', LOCATE('_', 'foo_bar_'))will give you the substring starting from the first underscore. If you want to get rid of that one, too, increment the locate-value by one.If you now want to replace the values in the tables itself, you can do this with an update-statement like
UPDATE table SET column = SUBSTR(column, LOCATE('_', column)).