I have the following json field
{
"Covid-19Vaccine Staus": "Not vaccinated (intent to in the future)",
"Date of last vaccine taken": "2021-08-09T00:00:00+04:00",
"If vaccinated, Name of vaccination received": "Other WHO Approved vaccine"
}
What i would like to do is update the key description i.e. Covid-19 Vaccine Staus to Covid19VaccineStaus.
On doing a direct update to the field on mysql workbench it generates the following query,
UPDATE `my_json_table` SET `containerValue` = '{\"Covid19VaccineStaus\": \"Vaccinated\", \"Date of last vaccine taken\": \"2021-07-13T00:00:00+04:00\", \"If vaccinated, Name of vaccination received\": \"Pfizer-BioNTech\"}' WHERE (`id` = '94');
where it looks like it takes the entire values for the field and then does the update.
What should the query look like if i want to update just the Covid19VaccineStatus key without putting in the values for the other data points for the json schema.
Please take a look at JSON functions
JSON_REPLACE,
Replace values in JSON document
JSON_REMOVE,
Remove data from JSON document
JSON_INSERT
Insert data into JSON document
UPDATE `my_json_table` SET `containerValue` = JSON_REPLACE(`containerValue`, '$."Covid-19Vaccine Staus"', 'Vaccinated') WHERE (`id` = '94');
UPDATE `my_json_table` SET `containerValue` = JSON_REMOVE(`containerValue`, '$."Covid-19Vaccine Staus"') WHERE (`id` = '94');
UPDATE `my_json_table` SET `containerValue` = JSON_INSERT(`containerValue`, '$."Covid-19Vaccine Staus"', 'Vaccinated') WHERE (`id` = '94');
To replace a key and keep value
UPDATE `my_json_table`
SET `containerValue` =
JSON_REMOVE(
JSON_INSERT(`containerValue`, '$."Covid19VaccineStaus"',
JSON_EXTRACT(`containerValue`, '$."Covid-19Vaccine Staus"')),
'$."Covid-19Vaccine Staus"')
WHERE (`id` = '94');
You can use JSON_SET which handles both insert and update actions.
UPDATE my_json_table
SET containerValue = JSON_SET(containerValue, '$."Covid-19Vaccine Staus"', 'Vaccinated')
WHERE id = 94;
So if your key does not exist yet in your JSON, it will be inserted with the value Vaccinated. Otherwise, the value corresponding to your key will be updated.
You can also find examples here on how to handle arrays or multiple values with JSON_SET.
If you only need to update the value but not perform any insertion in your JSON if the key does not exist, you can use JSON_REPLACE.
If you only need to insert the key and the value but not perform any update in your JSON if the key already exists, you can use JSON_INSERT.
If you want to update the name of your key:
UPDATE my_json_table
SET containerValue = JSON_INSERT(
JSON_REMOVE(containerValue, '$."Covid-19Vaccine Staus"'),
'$.Covid19VaccineStaus',
JSON_EXTRACT(containerValue, '$."Covid-19Vaccine Staus"')
)
WHERE id = 94;
I have a table "profil" and "kunde". "profil" got the email and password. "kunde" got the bio, username and email as index from "profil". "kunde" got the primary key username.
I want to update the username bio and passwort at once
UPDATE profil, kunde SET kunde.username = ?, kunde.bio = ?, profil.passwort = ? WHERE profil.email = ?;
the error code i got
#1062 - duplicated entry 'Kneipengänger60' for key 'PRIMARY'
You cannot update two tables in a single statement. However you can use transactions to ensure that the two statements are updated as a single unit. If one fails both are rolled back.
BEGIN TRANSACTION;
UPDATE profil
SET profil.passwort = ?
FROM profil, kunde
WHERE profil.id = kunde.id //Here you use the secondary key and primary keys to link the tables
and profil.email = ?;
UPDATE kunde
SET kunde.username = ?, kunde.bio = ?
FROM profil, kunde
WHERE profil.id = kunde.id //Here you use the secondary key and primary keys to link the tables
and profil.email = ?;
COMMIT;
How do i update only if a second condition is true, it seems my version of mysql doesnt allow a WHERE at the end of ON DUPLICATE syntax.
INSERT INTO `proxies` (`proxy`,`response`,`PAYMENT`,`type`,`country`,`status`,`tier`,`last_checked`,`last_active`,`response_time`)
VALUES ('111.9.204.96:8123','200','coolproxies','anon','China','active','3','1400624136','1400624136','1.577639')
ON DUPLICATE KEY UPDATE `response`='200',`response_time`='1.577639',`type`='anon',`country`='China',`status`='active',`tier`='2',`last_checked`='1400624137'
this works ok, but I need to only update when WHERE last_checked < '1400624137' is true.
This is what the query that does this looks like.
INSERT INTO `proxies` (`proxy`,`response`,`PAYMENT`,`type`,`country`,`status`,`tier`,`last_checked`,`last_active`,`response_time`)
VALUES ('207.204.249.193:21320','200','scanner','anon','United States','active','1','1400633866','1400633866','1.59696')
ON DUPLICATE KEY UPDATE
`response_time` = IF(`last_checked` < '1400633866', '1.59696', `response_time`),
`status` = IF(`last_checked` < '1400633866', 'active', `status`),
`last_checked` = IF(`last_checked` < '1400633866', '1400633866', `last_checked`),
`last_active` = IF(`last_checked` < '1400633866', '1400633866', `last_active`);
An alternative to doing it in 2 queries as #GordonLinoff mentioned, is to use the method described in this related question you can use an IF to update based on a condition.
INSERT INTO `proxies` (...)
VALUES (...)
ON DUPLICATE KEY UPDATE
my_column = IF(last_checked < '1400624137', VALUES(my_column), my_column)
Basically what the IF does is when TRUE then update the field to the new value, otherwise if FALSE set it to the current value, which means no change to your existing data.
I think you need to do this as two statements:
update . . .
where last_checked < '1400624137';
insert ignore into proxies(. . .);
(or use on duplicate key update to do nothing).
I have a complex query with a ON DUPLICATE KEY Update inside. I only want to insert the last value if there is no row with “timestamp_dag” = 1420070400, if there is a row with that condition I want to do nothing.
INSERT INTO data_prijzen_advertentie (
`ID_advertentie`,`jaar`,`rijnr`,`status_prijs`,`datum_dag`,`timestamp_dag`,
`prijs_maand`,`prijs_week`,`prijs_midweek`,`prijs_langweekend`,`prijs_weekend`,
`prijs_dag`,`prijs_ochtend`,`prijs_middag`
)
VALUES
(100,2014,1,1,'12-05-2014',1399852800,0,100,0,75,0,0,0,0),
(100,2014,2,1,'23-05-2014',1400803200,0,75,0,101,0,0,0,0),
(100,2014,3,1,'30-05-2014',1401408000,0,100,0,75,0,0,0,0),
(100,2014,4,1,'01-01-2015',1420070400,0,0,0,0,0,0,0,0)
ON DUPLICATE KEY UPDATE
status_prijs = VALUES(status_prijs), datum_dag = VALUES(datum_dag),
timestamp_dag = VALUES(timestamp_dag), prijs_maand = VALUES(prijs_maand),
prijs_week = VALUES(prijs_week), prijs_midweek = VALUES(prijs_midweek),
prijs_langweekend = VALUES(prijs_langweekend), prijs_weekend = VALUES(prijs_weekend),
prijs_dag = VALUES(prijs_dag), prijs_ochtend = VALUES(prijs_ochtend),
prijs_middag = VALUES(prijs_middag);
it's the best if you can handle this before the query string creation..
but if you can't, and your exceptional value is static, you can try something like this:
(I haven't tried to run it yet though)
INSERT INTO data_prijzen_advertentie (
`ID_advertentie`,`jaar`,`rijnr`,`status_prijs`,`datum_dag`,`timestamp_dag`,
`prijs_maand`,`prijs_week`,`prijs_midweek`,`prijs_langweekend`,`prijs_weekend`,
`prijs_dag`,`prijs_ochtend`,`prijs_middag`
)
VALUES
(100,2014,1,1,'12-05-2014',1399852800,0,100,0,75,0,0,0,0),
(100,2014,2,1,'23-05-2014',1400803200,0,75,0,101,0,0,0,0),
(100,2014,3,1,'30-05-2014',1401408000,0,100,0,75,0,0,0,0),
(100,2014,4,1,'01-01-2015',1420070400,0,0,0,0,0,0,0,0)
ON DUPLICATE KEY UPDATE
status_prijs = IF(timestamp_dag<>1420070400, VALUES(status_prijs), status_prijs),
datum_dag = IF(timestamp_dag<>1420070400, VALUES(datum_dag), datum_dag),
timestamp_dag = IF(timestamp_dag<>1420070400, VALUES(timestamp_dag), timestamp_dag),
prijs_maand = IF(timestamp_dag<>1420070400, VALUES(prijs_maand), prijs_maand),
prijs_week = IF(timestamp_dag<>1420070400, VALUES(prijs_week), prijs_week),
prijs_midweek = IF(timestamp_dag<>1420070400, VALUES(prijs_midweek), prijs_midweek),
prijs_langweekend = IF(timestamp_dag<>1420070400, VALUES(prijs_langweekend), prijs_langweekend),
prijs_weekend = IF(timestamp_dag<>1420070400, VALUES(prijs_weekend), prijs_weekend),
prijs_dag = IF(timestamp_dag<>1420070400, VALUES(prijs_dag), prijs_dag),
prijs_ochtend = IF(timestamp_dag<>1420070400, VALUES(prijs_ochtend), prijs_ochtend),
prijs_middag = IF(timestamp_dag<>1420070400, VALUES(prijs_middag), prijs_middag);
You will only have the ON DUPLICATE KEY UPDATE ... portion run if the timestamp_dag matches the criteria for triggering it. From the MySQL docs:
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that
would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an
UPDATE of the old row is performed.
So assuming your table is built this way, then the update will trigger.
You can add a UNIQUE index using the syntax as described here.
CREATE UNIQUE INDEX ind_unique_timestamp_dag ON data_prijzen_advertentie (timestamp_dag)