Remember how to delete a certain character in a field in MySQL

Problem Description:

Problem: When modifying and deleting the specified characters in the comma-separated string in the project, other characters cannot be used.

Cause Analysis:

The SQL statement is misspelled. The third parameter in replace() is omitted, and it is omitted. Wrong way of writing:
    update 表名
    set 字段名= TRIM(BOTH ',' FROM replace(concat(',', 字段名, ','), #{传入的字符}, ''))

In mybatis, the correct dynamic SQL is as follows:

    update 表名
    set 字段名= TRIM(BOTH ',' FROM replace(concat(',', 字段名, ','), #{传入的字符}, ','))

ps: The incoming character is the character that needs to be deleted. It should be noted that commas need to be spliced ​​before and after this character. If the character 12 in devid in the solution needs to be deleted, the incoming character should be: ',12, ' .


solution:

After comparing the following sql, a syntax error is found.
How to delete a character in a field in mysql
If the table name is user, the table is as follows

id david
1 1,2,3,12,13,14

To delete the value of 1 in the devid field of the table user, note that 12, 13, and 13 cannot be deleted.

The sql syntax is as follows:

update user set devid = trim(both ‘,’ from replace(concat(’,’, devid, ‘,’), ‘,1,’, ‘,’))

Excerpted from (deleted): https://www.cnblogs.com/xiede/p/9369578.html

Guess you like

Origin blog.csdn.net/qq_37196265/article/details/122062732