Is there a JSON function in mysql that will ignore trying to add the element if it already exists? For example:
update waitinglist SET
new = JSON_ARRAY_APPEND(new, '$', "orange")
where id=2;
update waitinglist SET
new = JSON_ARRAY_APPEND(new, '$', "orange")
where id=2;
Now my array looks like:
["apple", "orange", "orange", "orange", "orange"]
But I want it to work like a set, and just be:
["apple", "orange"]
Is there a way to do this?
I don't think so. You can test whether the value is already in the JSON in the WHERE clause.
update waitinglist SET
new = JSON_ARRAY_APPEND(new, '$', '"orange"'))
where id=2
AND NOT JSON_CONTAINS(new, '"orange"')
If you're updating multiple columns and need this to affect just this one column, you can use IF() to leave it unchanged if the value is already there.
update waitinglist SET
new = IF(JSON_CONTAINS(new, '"orange"'), new, JSON_ARRAY_APPEND(new, '$', '"orange"'))
where id=2
Related
I need to set/update JSON array in MYSQL table from Node and I have this query, that throws a Invalid JSON path expression error.
For example I want to find object with key 2022-01-03 and if it exist update its value to O 08:00
UPDATE allemployees SET schedule = JSON_SET(schedule, '$.2022-01-03', 'O 08:00') WHERE name_cyr = 'John Doe'
My JSON in the table looks like this:
[{"2022-01-03": "H 08:00"}, [{"2022-01-04": "H 08:00"}] ]
UPDATE allemployees
SET schedule = JSON_SET(schedule, '$[0]."2022-01-03"', 'O 08:00')
WHERE name_cyr = 'John Doe';
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=2c59600049b4dfc1675c444a6da578bb
The path "2022-01-03" contains dashes and must be enclosed with doublequote chars.
The value to be set is not upper-level value, it is a component of definite array element.
So I am trying to update some entries in my postgres database. In particular I am trying to modify a field value using the existing value. For example, say I have the following json
{"var1": 10, "var2": 0.003, "var3": null}
and I want to update var2 to var2*100. I have updated values using an update statemnt, e.g.
UPDATE my_table SET json_column = jsonb_set(my_column, '{var2}', '0.003', true) WHERE (my_column->'var2') is null;
so I am trying to use an equivalent statement
UPDATE my_table SET my_column = jsonb_set(json_column, '{var2}', '(json_column->'var2)::double precision*100', true) WHERE id = 12;
however I am facing syntax errors. Has anyone tried something like this?
This can get really tricky but you can get there with some casts. It worked for me like this:
UPDATE my_table
SET my_column = jsonb_set(json_column, '{var2}', to_jsonb((json_column->'var2')::double precision*100), true)
WHERE id = 12;
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 a table with JSON type column, I want to update a column with new array element in existing JSON.
What needs to be done: add an array in JSON column when employee punch_in and add another array in JSON column when employee punch_out.
{"emp_sheet":[{"rulecode":"PUNCH_IN","result":1,"applytime":"2018-04-12 04:50:39"},{"rulecode":"PUNCH_OUT","result":1,"applytime":"2018-04-12 13:01:39"}]}
What I did, for employee punch_in:
UPDATE table
SET rule_codes = JSON_SET(COALESCE(rule_codes, '{}'), '$.emp_sheet', '{"rulecode":"PUNCH_IN","result":1,"applytime":"2018-04-12 04:50:39"}')
WHERE emp_id = 1
Result in rule_codes column =
{"emp_sheet": "{"rulecode":"PUNCH_IN","result":1,"applytime":"2018-04-12 04:50:39"}"}
Please help me to write update query for employee punch_out.
This would be easiest if you made $.emp_sheet a JSON array on punch in:
UPDATE table3
SET rule_codes = JSON_SET(COALESCE(rule_codes, JSON_OBJECT('emp_sheet', JSON_ARRAY())),
'$.emp_sheet[0]',
'{"rulecode":"PUNCH_IN","result":1,"applytime":"2018-04-12 04:50:39"}')
WHERE emp_id = 1
Then on punch out, you can add another element to the array:
UPDATE table3
SET rule_codes = JSON_SET(COALESCE(rule_codes, JSON_OBJECT('emp_sheet', JSON_ARRAY())),
'$.emp_sheet[1]',
'{"rulecode":"PUNCH_OUT","result":1,"applytime":"2018-04-12 13:01:39"}')
WHERE emp_id = 1;
SELECT rule_codes FROM table3 WHERE emp_id = 1
Output:
{"emp_sheet": [
"{\"rulecode\":\"PUNCH_IN\",\"result\":1,\"applytime\":\"2018-04-12 04:50:39\"}",
"{\"rulecode\":\"PUNCH_OUT\",\"result\":1,\"applytime\":\"2018-04-12 13:01:39\"}"
]}
Note that when you do the SET, the input JSON ('{"rulecode ... }') gets treated as a string, hence the escaped " in the output above. You can remove those with JSON_UNQUOTE when you extract i.e.
SELECT JSON_UNQUOTE(JSON_EXTRACT(rule_codes, '$.emp_sheet[0]')) FROM `table3`
or using the short-cut notation
SELECT rule_codes->>'$.emp_sheet[0]' FROM `table3`
Output:
{"rulecode":"PUNCH_IN","result":1,"applytime":"2018-04-12 04:50:39"}
Try to use JSON_ARRAY_APPEND instead of JSON_SET.
Manual - https://dev.mysql.com/doc/refman/8.0/en/json-modification-functions.html
I think it could be like this
rule_codes = JSON_ARRAY_APPEND(COALESCE(rule_codes, '{"emp_sheet":[]}'), '$.emp_sheet', '{"rulecode":"PUNCH_IN","result":1,"applytime":"2018-04-12 04:50:39"}')
or
rule_codes = IF(rule_codes IS NULL,'
'{"emp_sheet":[{"rulecode":"PUNCH_IN","result":1,"applytime":"2018-04-12 04:50:39"}]}',
JSON_ARRAY_APPEND(rule_codes, '$.emp_sheet', '{"rulecode":"PUNCH_IN","result":1,"applytime":"2018-04-12 04:50:39"}')
)
How can i update all the fields from a certain row within Mysql database .
By DB is : bookinga_Hotels
The Tabele is : HotelList
The column with the value that i need to update is : HotelImages
And i want to update all the rows where "image.metglobal.com" with "bookingassist.ro"
Have any ideea on how to do this?
You can use as
update HotelList
set HotelImages = 'bookingassist.ro'
where HotelImages = 'image.metglobal.com'
If you have mix of strings along with the image url then you can use replace
update HotelList
set HotelImages = replace(HotelImages,'image.metglobal.com','bookingassist.ro');
All you need to do is to execute this MySQL query :
update HotelList
set HotelImages = 'bookingassist.ro'
where HotelImages = 'image.metglobal.com'