1

I have a table called product_option_param that has a column povs of JSON type, which basically keeps JSON arrays with ids (for example, [1,2,3])

When I need to delete, for example, an id 1, I execute this:

UPDATE product_option_param 
SET povs = JSON_REMOVE(
   povs, replace(JSON_SEARCH(povs, 'one', 1), '"', '')
) 
WHERE json_search(povs, 'one', 1) IS NOT NULL

But it just turns all cells that have 1 in the array into null. What am I doing wrong?

1 Answer 1

1

Try:

SET @`id` := '1';

UPDATE `product_option_param`
SET `povs` =
  JSON_REMOVE(
    `povs`,
    JSON_UNQUOTE(
      JSON_SEARCH(
        REPLACE(
          REPLACE(
            REPLACE(
              `povs`,
              '[',
              '["'
            ),
            ']',
            '"]'
          ),
          ',',
          '","'
        ),
      'one',
      @`id`
    )
  )
)
WHERE
  JSON_CONTAINS(`povs`,  @`id`);

See db-fiddle.

Sign up to request clarification or add additional context in comments.

Comments

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.