How add to json varible a new element in mySQL
From this:
Value = ['123']
Make this:
Value = ['123','456']
JSON_ARRAY_APPEND() function
SET #Value = '["123"]';
SET #Value = JSON_ARRAY_APPEND(#Value, '$', '456');
SELECT #Value;
#Value
["123", "456"]
fiddle
Related
I have tried:
SET #my_var = 'one,two,three';
SET #to_replace = 'two,';
SET #my_check = 1;
SET #my_var = SELECT IF(
ISNULL(#my_check),
#my_var,
REPLACE(#my_var, #to_replace,'')
);
I have tried also SET #my_var = IF( ... Or SELECT REPLACE instead REPLACE in condition
But got syntax error
What I expect is:
If #my_check = null so #my_var = 'one,two,three' (still unchanged)
Else replace two, with '' but using #to_replace variable
Expected result: #my_var = 'one,three'
set #my_var = if(isnull(#my_check), #my_var, replace(#my_var, #to_replace, ''));
Works well in MySQL v8.
I need to update a json value in a column as well as update another column in the same query.
Something like this:
UPDATE fixtures
SET jsonResults = '{}',
JSON_SET(jsonFixture, '$.time_status', '0')
WHERE intRefID = 88961323;
How can I accomplish this?
JSON_SET() returns a JSON document value, but an UPDATE statement needs a series of assignment expressions:
UPDATE fixtures
SET jsonResults = '{}',
jsonFixture = JSON_SET(jsonFixture, '$.time_status', '0')
WHERE intRefID = 88961323;
This replaces jsonFixture with the result of JSON_SET(), after setting a field within that document.
Compare with an UPDATE like this:
UPDATE mytable
SET i = i + 1
WHERE ...
It takes the value of i, adds 1, and then uses the result of that addition expression to replace i.
I have column which has values like so foo,bar in mySql Database.
I would like to take out only the foo and discard the rest of the data after comma. How do I get that?
You are looking for substring_index():
select substring_index(col, ',', 1)
You can use Split() function.
Example:
var original_Data = foo,bar ;
var required_Data = original_Data.Split(',')[0];
this will result required_Data = foo;
If you want a "bar" then
var required_Data = original_Data.Split(',')[1];
this will result required_Data = bar;
I'm looking for something like forEach for a JSON array in MySQL.
I manager IDs in MySQL JSON data type like this: [1, 2, 3, 4, 5], and I want to perform an action for each item in the list.
A naive solution is to do a WHILE loop with a counter that starts at 0 and ends when VAR_MANAGER_ID is null. Here is a contrived example of how the inside of the WHILE loop would look:
SET VAR_PATH = CONCAT('$[', COUNTER, ']');
SET VAR_MANAGER_ID = JSON_PARSE(VAR_MANAGER_IDS, PATH);
# See if we've reached the end of the list
IF VAR_MANAGER_ID IS NULL
THEN
BREAK
END;
INSERT INTO LU_MANAGER (MANAGER_ID) VALUES (VAR_MANAGER_ID);
But there has to be a better way! How can I do something like:
FOREACH JSON_PARSE(VAR_MANAGER_IDS, '$[*]') AS VAR_MANAGER_ID
INSERT INTO LU_MANAGER (MANAGER_ID) VALUES (VAR_MANAGER_ID);
complete newbie here but I found a way to iterate a JSON array using REPEAT-UNTIL-END REPEAT
REPEAT
SET txt = JSON_EXTRACT(myjson, CONCAT("$[", indx, "]"));
# use txt
SET indx = indx + 1;
UNTIL indx = JSON_LENGTH(myjson)
END REPEAT;
I have wrote a stored proc in sqlyog. It is quite long and performing all the desired functionality except for the concat statement so I will only list that particular query.
UPDATE recipes_new
SET co_auths = CONCAT(co_auths,',',c1id)
WHERE id = name_in;
I basically want a separation in the two fields and this statement is placed in a cursor so it is iterative. co_auths is null at the moment so I get the result as ,1,2,3 where as I want it to be 1,2,3. Any guesses what can the most appropriate solution be?
By using an IF:
UPDATE recipes_new
SET co_auths = IF(co_auths IS NULL, c1id, CONCAT(co_auths, ',', c1id))
WHERE id = name_in;
If the value of co_auths is an empty string instead of NULL:
UPDATE recipes_new
SET co_auths = IF(LENGTH(co_auths), CONCAT(co_auths, ',', c1id), c1id)
WHERE id = name_in;
MySQL returning an empty field: CONCAT(nonEmpty1,empty2,nonEmpty3) = NULL
CONCAT_WS is what you are looking for
UPDATE recipes_new SET co_auths = CONCAT_WS(co_auths,',',c1id) WHERE id = name_in;
This should work using CASE to check if it's NULL:
CONCAT(
CASE
WHEN IFNULL(co_auths,'') = ''
THEN ''
ELSE CONCAT(co_auths, ',')
END, c1id)