我的一个表有一个字段 tags
和字段的值,如 {2,3,8}
tag_id | r_tags
--------|------------
1 | {2,3,8}
--------|------------
2 | {5,8}
我想更新字段而不删除当前值。对于前。如果我需要为标签 ID 1
添加 5
,则 {2,3,8} 应该类似于 {2,3,8,5}
现在它的工作方式如下
1. Select the current value.
2. Add 5 using php and created new string.
3. Update the row.
我只想知道只能通过MySql可以吗?
最佳答案
这应该有效 -
假设你想在 tag_id = 1
中的 r_tags
中添加 5,这应该是查询 -
UPDATE tags SET r_tags = CONCAT(SUBSTRING(r_tags, 1, CHAR_LENGTH(r_tags) - 1),',5','}') WHERE tag_id = 1;
在 php 中,
// $new_val and $tag_id as variables.
$query = "UPDATE tags SET r_tags = CONCAT(SUBSTRING(r_tags, 1, CHAR_LENGTH(r_tags) - 1),',$new_val','}') WHERE tag_id = $tag_id"
关于php - 如何在不删除mysql中当前值的情况下更新行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21568717/