0

My sql structure has a field which is array.
The value is

{5010011,5010031,5010041,5010081}

I execute a query.

Update users 
  set faces=uniq(array_cat(faces,ARRAY[5010011,5010031,5010041,5010081]))

I get the result is

{5010011,5010031,5010041,5010081,5010011,5010031,5010041,5010081}

I use the uniq() function, but it does not work. I want to remove the duplicate value in array.

1 Answer 1

1

Quote from the manual

uniq(int[]) int[] remove adjacent duplicates

(emphasis mine)

So it only removes duplicates that are adjacent (a value immediately followed by the same value).

The manual also shows how to use the function: you need to sort the array first, so that the duplicate values wind up next to each other: uniq(sort('{1,2,3,2,1}'::int[]))

So in your case:

Update users 
  set  faces = uniq(
                   sort(array_cat(faces, ARRAY[5010011,5010031,5010041,5010081]))
                   )
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for quick reply. I don't sort the array. So the uniq function is not work.
@WongManlok: well, you have to sort the array before applying the uniq() function as I have shown in my answer. The better solution however would be to normalize your data model and then create a proper unique index

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.