Comments - FIND_IN_SET

9 months, 4 weeks ago Android Marchand

if you wish to update a record and remove an element from a SET, you can't use UPDATE my_table SET my_col = my_col - 'element_to_remove' and you can't use UPDATE my_table SET my_col = my_col - FIND_IN_SET('element_to_remove',my_col)

Rather, you must use bit arithmetic this way: UPDATE my_table SET my_col = my_col - floor(pow(2,find_in_set('element_to_remove',my_col)-1))

Here is a breakdown: 1. find_in_set() will return the position of 'element_to_remove' in my_col, starting at 1 2. since bit arithmetic is starting at 0, you must remove 1 3. converting that number to a power of 2 with pow() 4. taking the floor in case 'element_to_remove' isn't present in my_col to start with, which gives pow(2,-1) = 0.5 so the floor of that is zero

 
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.