简体   繁体   中英

MySQL Remove part of string after specific character

I do got a database named "MAPPINGS" with about 23.000 rows I need to replace becouse of a mistake. Is there any query to achieve the following:.

UPDATE MAPPINGS SET MAIN = 'firt part' WHERE USERID = '1578' AND MAIN LIKE 'first part >%'

The problem is that "first part" is every time something else. I just need to remove everyhing after the ">" than the MAPPINGS are correct.

Or can this only be done by a PHP script? while select * from mappings where userid = '1578' and then the update query. I hope there will be a query to achieve this.

You can do this in MySQL using substring_index() :

UPDATE MAPPINGS
    SET MAIN = SUBSTRING_INDEX(MAIN, '>', 1)
    WHERE MAIN LIKE '%>%' AND USERID = '1578';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM