Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: How do I update a value inside of an ltree?

As the title suggests, I'm having a hard time, guessing how I can efficiently update a value in multiple rows. The column is implemented with the data-type ltree. So, there should be some form of special operations available for this data type in postgresql Here's an example:

Name of the table, let's say: product_sections and the name of the column containing these values is section_path (type: ltree)

Android
Android.Browser
Android.Browser.Test
Android.Browser.Compare.URL

Here, I want to update only the Browser to something like Foo part so that, the data will become like this:

Android
Android.Foo
Android.Foo.Test
Android.Foo.Compare.URL

Thanks in advance :)

like image 655
Sazid Avatar asked Nov 01 '22 10:11

Sazid


1 Answers

update product_sections
set product_path = 'Android.foo'::lpath|| subpath(product_sections,2)) 
where product_path <@ 'Android.browser'::lpath

untested, but this should achieve the correct result.

like image 166
Joe Love Avatar answered Nov 15 '22 05:11

Joe Love