If you have the primary key in table then the below SQL will really helpful to you:
Your SQL should like this as per my best tries:
update fruits f
left join (
select id,group_concat(fruit) new_fruit_data from(
SELECT id,SUBSTRING_INDEX(SUBSTRING_INDEX(t.fruit_data, ',', x.cifre), ',', -1) AS fruit
,count(1)
FROM (
SELECT id,fruit_data FROM fruits) t
INNER JOIN
(
SELECT 1 + a.i + b.i * 10 cifre, b.i + a.i * 10 sute
FROM (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) a
CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) b
) x
ON (length(fruit_data)-length(replace(fruit_data,',',''))+1) >= x.cifre
group by id,SUBSTRING_INDEX(SUBSTRING_INDEX(t.fruit_data, ',', x.cifre), ',', -1)) k
where fruit not rlike 'pear'
group by id) n
on f.id = n.id
set f.fruit_data = n.new_fruit_data;
Explaination in Details:
Setting up table:
create table fruits(id int,fruit_data varchar(500));
insert into fruits values(1,'greenApple,greePear,redApple');
insert into fruits values(2,'greePear');
insert into fruits values(3,'redApple,orangePear');
insert into fruits values(4,'greenApple,redApple');
insert into fruits values(5,'yellowPear,greenApple,greenPear');
Your base table data.
mysql> select * from fruits;
+------+---------------------------------+
| id | fruit_data |
+------+---------------------------------+
| 1 | greenApple,greePear,redApple |
| 2 | greePear |
| 3 | redApple,orangePear |
| 4 | greenApple,redApple |
| 5 | yellowPear,greenApple,greenPear |
+------+---------------------------------+
5 rows in set (0.00 sec)
Here is the workaround and your solution:
mysql> update fruits f
-> join (
-> select id,group_concat(fruit) new_fruit_data from(
-> SELECT id,SUBSTRING_INDEX(SUBSTRING_INDEX(t.fruit_data, ',', x.cifre), ',', -1) AS fruit
-> ,count(1)
-> FROM (
-> SELECT id,fruit_data FROM fruits) t
-> INNER JOIN
-> (
-> SELECT 1 + a.i + b.i * 10 cifre, b.i + a.i * 10 sute
-> FROM (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) a
-> CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) b
-> ) x
-> ON (length(fruit_data)-length(replace(fruit_data,',',''))+1) >= x.cifre
-> group by id,SUBSTRING_INDEX(SUBSTRING_INDEX(t.fruit_data, ',', x.cifre), ',', -1)) k
-> where fruit not rlike 'pear'
-> group by id) n
-> on f.id = n.id
-> set f.fruit_data = n.new_fruit_data;
Query OK, 3 rows affected (0.05 sec)
Rows matched: 4 Changed: 3 Warnings: 0
Output, after update:
mysql> select * from fruits;
+------+---------------------+
| id | fruit_data |
+------+---------------------+
| 1 | greenApple,redApple |
| 2 | NULL |
| 3 | redApple |
| 4 | greenApple,redApple |
| 5 | greenApple |
+------+---------------------+
5 rows in set (0.00 sec)