简体   繁体   中英

Delete on of the id in mysql varchar

I cannot find the way how i can delete one of the stored ids in varchar column.

id         fieldid
1          ,5,13,15,66,443,

how i can delete, lets say 15 from this field id?

The correct logic is:

UPDATE <your_table_name>
    SET fieldid = REPLACE(fieldid, ',15,', ',')
---------------------------------------^----^ these commas a really important
    WHERE id = 1;

However, storing list of numbers of strings is bad, bad, bad in SQL:

  • Numbers should be declares as numbers, not strings.
  • Foreign key relationships should be explicitly declared.
  • SQL has lousy string processing functionality.
  • Queries on string lists cannot be optimized.
  • And there are more reasons.

Sometimes, you are stuck with someone else's really, really, really bad design decisions.

You gave give REPLACE a shot though

DECLARE @Test VARCHAR(50)

SET @Test = ',5,13,15,66,443,'

SELECT REPLACE(@Test,'15,','')

Result

,5,13,66,443,

Check with ORACLE

UPDATE <your_table_name>
  SET fieldid = REPLACE(fieldid,',15','')
 WHERE id = 1;

This will replace ',15' with empty string. Like remove from string.

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