[英]To append comma separated value no duplicates in mysql update Query
I am storing table data as comma separated values like this 我将表数据存储为这样的逗号分隔值
id name fk_id
1 abc 2,4,6
2 def 2,7,8
Now, I want to use CASE WHEN or some function in Update Query of MySQL which updates the field if no duplicate value: 现在,我想在MySQL的Update Query中使用CASE WHEN或某些函数,如果没有重复值,则更新字段:
For example, same value for fk_id then ignores else update like if 10 for id 1 then update but if 7 for id 2 then ignore 例如,fk_id的相同值然后忽略其他更新,如果10为id 1然后更新,但如果7为id 2然后忽略
id name fk_id
1 abc 2,4,6,10
2 def 2,7,8
I tried the following code: 我尝试了以下代码:
if(
find_in_set($fk_id,fk_id),
fk_id,
CONCAT_WS(',', fk_id, $fk_id)
)
But not working. 但不行。
Just update your code like as 只需更新您的代码即可
set fk_id = if(find_in_set($fk_id,fk_id),
fk_id,
CONCAT(fk_id, ',', $fk_id)
)
Try this : 试试这个 :
$sql = "
UPDATE `test`
SET `fk_id` = CONCAT(fk_id, ',','" . $fk_id . "')
WHERE `fk_id` REGEXP ',?(" . $fk_id . "),?'
";
NOTE : REGEXP is not the best option when it comes to speed. 注意:在速度方面,REGEXP不是最佳选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.