Mass update values from the rown in MYSQL - mysql

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'

Related

Modifying existing field in json column in Postgres

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;

mysql update json attribute and another column in one query

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.

mysql concatenate and increment by one

I am trying to update mysql table MYTABLE using two value. One is STAR column which should be incremented by one on each query, and the second one is COMMENT column which should be concatenated with existing one on each time and separated by comma.
Below is the command I used, but not working.
$query = "update MYTABLE set STAR=STAR+1,COMMENT= CONCAT(COMMENT, ','.$comment) where ID='$id'";
$query = "update MYTABLE set STAR=STAR+1,COMMENT = CONCAT(COMMENT, ',', '$comment') where ID=$id";
where ID='$id'
is incorrect because $id might be a number, so, delete the "'".
Have you escaped the $comment variable ?
Otherwise you may use prepared statements with PDO :)
I hope you're using PDO...
you should but string in '' and update your query, it has error syntax :
$query = "update MYTABLE set STAR=STAR+1,COMMENT= CONCAT(COMMENT, '$comment') where ID='$id'";
To make it more secure, just use following code...
$query = "update MYTABLE
set `STAR` = `STAR`+1,
`COMMENT`= CONCAT(COMMENT, '$comment')
where `ID`='$id'";
Happy Coding...

How to make a massive selected update

I have a list of values(deviceID) and I need to make an MySQL Query to update a column value, I can do the query in the following way
UPDATE Clients.Devices
SET assignedUserID='Jhon'
WHERE accountID='Delivery1' AND
(deviceID='1234' OR deviceID='1235' OR deviceID='1236')
That's is a simple example but some accountID have more than 500 devicesID so I'm looking for a shorter SQL statement
So, There's any way to pass the list of deviceID's to the SQL server?
Something like
UPDATE Clients.Devices
SET assignedUserID='Jhon'
WHERE accountID='Delivery1' AND
(deviceID=('1234','1235','1236))
Thanks.
UPDATE
if the datatype of deviceID is numberic, single quotes are not required.
maybe IN for multiple values not =
UPDATE Clients.Devices
SET assignedUserID = 'Jhon'
WHERE accountID = 'Delivery1' AND deviceID IN (1234,1235,1236)
or if the ID are in sequence use BETWEEN
UPDATE Clients.Devices
SET assignedUserID = 'Jhon'
WHERE accountID = 'Delivery1' AND deviceID BETWEEN 1234 AND 1236
You may try this too:
UPDATE Clients.Devices
SET assignedUserID='Jhon'
WHERE accountID='Delivery1' AND deviceID > = 1234 AND deviceID <= 1236
;
Maybe this will work, if using php:
$ids = join(',',$deviceIDs);
$sql = "UPDATE Clients.Devices
SET assignedUserID='Jhon'
WHERE accountID='Delivery1' AND deviceID IN ($ids)";

How to use concat() to append to multiple columns in a single mysql update

I can't figure out the syntax for Mysql update with multiple concatinations. I want to be able to append a string to the end of the string stored in the database but do it to multiple columns all at once. I can do one column at a time just fine with this
UPDATE `table1`.`column1` SET `category1` = CONCAT(category1,'$value[0]',) WHERE `id`='$id';
But when I try to do it to multiple columns in the same table I get a syntax error.
UPDATE `table1`.`column1`
SET `category1` = CONCAT(category1,'5'),
`category2` = CONCAT(category2,'5'),
`category3` = CONCAT(category3,'5'),
`category4` = CONCAT(category4,'5'),
`category5` = CONCAT(category5,'5'),
`comments` = CONCAT(comments, 'jfsaklfsad')
WHERE `for_student_id`='46';
"You have an error in your SQL syntax;"
I can't find the syntax for separating each concat.
According to MySQL docs, UPDATE does not support such syntax. You must reference the table name, without the column, before the SET:
UPDATE `table1`
SET `category1` = CONCAT(category1,'5'),
`category2` = CONCAT(category2,'5'),
`category3` = CONCAT(category3,'5'),
`category4` = CONCAT(category4,'5'),
`category5` = CONCAT(category5,'5'),
`comments` = CONCAT(comments, 'jfsaklfsad')
WHERE `for_student_id`='46';