简体   繁体   English

删除mysql varchar中的ID

[英]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. 我找不到删除varchar列中存储的ID之一的方法。

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

how i can delete, lets say 15 from this field id? 我该如何删除,可以说这个字段ID为15?

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: 但是,在SQL中存储字符串数列表是不好的,不好的,不好的:

  • Numbers should be declares as numbers, not strings. 数字应声明为数字,而不是字符串。
  • Foreign key relationships should be explicitly declared. 外键关系应明确声明。
  • SQL has lousy string processing functionality. SQL具有糟糕的字符串处理功能。
  • 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 您虽然给了REPLACE一个机会

DECLARE @Test VARCHAR(50)

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

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

Result 结果

,5,13,66,443,

Check with ORACLE 用ORACLE检查

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

This will replace ',15' with empty string. 这将用空字符串替换',15'。 Like remove from string. 就像从字符串中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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