-2

I'm using MariaDB version 10.3.39. I have create a table named "sys_config" with multiple columns. A column name is "json_system". It's data type is assigned to "longtext". Using this reference: https://mariadb.com/kb/en/json_replace/

I have inserted { "A": 1, "B": [2, 3]} in this column.

When I query

SELECT json_system FROM sys_config

everything goes ok.

OK

But when I try to query:

SELECT JSON_REPLACE(SELECT json_system FROM sys_config, '$.B[1]', 4)

A Syntax Error will be occurred: Syntax Error

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT json_system FROM sys_config, '$.B[1]', 4) LIMIT 0, 25' at line 1

Why this happen? According to MariaDB reference when I query: SELECT JSON_REPLACE('{ "A": 1, "B": [2, 3]}', '$.B[1]', 4); the query will be successful:

OK What's wrong with the second query? When I pass the "SELECT" result into the "JSON_REPLACE" function a syntax error will be appeared.

I have tried below query: SELECT JSON_REPLACE('{ "A": 1, "B": [2, 3]}', '$.B[1]', 4);

This works.

But the query:

SELECT JSON_REPLACE(SELECT json_system FROM sys_config, '$.B[1]', 4)

doesn't work.

2
  • the query make no sense at all, the inner select will return a lot of rows, but json_repace needs there a json not a bunch of rows Commented Jan 12, 2024 at 21:15
  • @nbk the table only has one row. It does not make a difference whether the database table has one row or several rows. The syntax is incorrect. Commented Jan 13, 2024 at 6:16

1 Answer 1

-1

When using a SELECT query as an expression, it must be enclosed in an [extra] pair of parens:

SELECT JSON_REPLACE((SELECT json_system FROM ...))
                    ^                           ^
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This query SELECT JSON_REPLACE((SELECT json_system FROM sys_config), '$.B[1]', 4) worked for me. What I noticed is that if I use SELECT JSON_REPLACE(SELECT json_system FROM sys_config, '$.B[1]', 4) then the MariaDB will considers sys_config, as an extension of the expression, not the second argument of JSON_REPLACE.

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.