简体   繁体   中英

How to search for string from one column in other column and delete?

Let's say I have two columns in my MySQL database called first and second .

I want to search in second for the value of first an delete it.

Example:

First row: first is: test , second is: Hello, this is a test!

Expected result: second is: Hello, this is a !

I think it shouldn't be difficult but I don't know how to do.

Can anybody help me?

For querying, this should do it.

SELECT
    REPLACE(second, first, '')
FROM your_table;

For updating:

UPDATE your_table SET second = REPLACE(second, first, '');

As mentioned by @Gordon in the comments below, add a where clause to do the update on the required rows only:

UPDATE your_table SET second = REPLACE(second, first, '')
WHERE second like concat('%', first, '%');

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