문제

I have the following table structure:

table "sample"

objectID | objectValues
       1 | 5,6,7
       2 | 6,7,5,8
       3 | 5
       4 | 7,8,9,5,6
       5 | 10,11
       6 | 5

So, I want to delete all ",5" or "5," values from "objectValues" and delete all columns which theirs "objectValues" value is only "5". How can I do this?

도움이 되었습니까?

해결책

You can try a tricky method using four queries to make this change.

first query for delete all rows when object value is 5

DELETE FROM sample WHERE objectValues = 5;

second for update rows when is a value is in all places except first and last

UPDATE sample 
SET objectValues = REPLACE(objectValues , ',5,', ',');

third when the value 5 is on the first place

UPDATE sample 
SET objectValues = LEFT(objectValues, LENGTH(objectValues)-2)
WHERE objectValues LIKE '5,%';

and last query when the value 5 is on the last place

UPDATE sample 
SET objectValues = RIGHT(objectValues, LENGTH(objectValues)-2)
WHERE objectValues LIKE '%,5';

다른 팁

Try this

delete from table where find_in_set(5,objectValues)>0;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top