3

Imagine I have a table which has a Params column and it contains settings of my gallery. I want to add 2 other properties automatically with MySQL query. I have already test this query but it will add my new properties at the first of the column value which is incorrect :

UPDATE table 
SET params=CONCAT(',  "slider_fullscreen_button_skin": "myTheme", "slider_zoompanel_skin": "myTheme"',params)
WHERE params NOT LIKE '%myTheme%';

let's say I have this value on my column :

{"title":"Bessariabian","alias":"Bessariabian","category":"1103","full_width":"false"} 

I want my value change to something like this:

{"title":"Bessariabian","alias":"Bessariabian","category":"1103","full_width":"false", "slider_fullscreen_button_skin": "myTheme", "slider_zoompanel_skin": "myTheme"}

I want to make a query which adds the new properties at the end of the value before } location. How I can handle this?

2
  • 2
    Please add some sample data and expected output Commented Oct 5, 2018 at 10:47
  • OK, let's say I have this value on my column : {"title":"Bessariabian","alias":"Bessariabian","category":"1103","full_width":"false"} I want my value change to something like this: {"title":"Bessariabian","alias":"Bessariabian","category":"1103","full_width":"false", "slider_fullscreen_button_skin": "myTheme", "slider_zoompanel_skin": "myTheme"} Commented Oct 5, 2018 at 10:56

1 Answer 1

1
  • Use Substring_Index() function to get the Substring before the first occurence of }.
  • Now, Concat() this substring with your required string, and } at the end.

Try:

UPDATE table 
SET params = CONCAT(
                    SUBSTRING_INDEX(params, '}', 1), 
                    ',  "slider_fullscreen_button_skin": "myTheme", "slider_zoompanel_skin": "myTheme"', 
                    '}'
                   )
WHERE params NOT LIKE '%myTheme%';
1
  • @BabakHabibi happy to help :) Commented Oct 5, 2018 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.