Replacing data in database - mysql

I get a sheet of data that gets updated values daily, I am trying to write the updated data daily into the database to replace the data from the previous day. The database is Mysql. Also is it possible to use the replace function to do this?

Not sure if I fully understand your question, but to replace data you need a key for each row in your sheet.
Than you should
UPDATE <target_table> SET column1 = value1_from_sheet
WHERE key = key_col_from_sheet
REPLACE is a string function in TSQL and you can not use it to replace rows in a table. It is useful to replace some chars inside a string.
DECLARE #string nvarchar(50);
SET #string = "someValue";
SELECT REPLACE(#string,'Va','XX') => 'someXXlue'

Related

Rails - How to reference model's own column value during update statement?

Is it possible to achieve something like this?
Suppose name and plural_name are fields of Animal's table.
Suppose pluralise_animal is a helper function which takes a string and returns its plural literal.
I cannot loop over the animal records for technical reasons.
This is just an example
Animal.update_all("plural_name = ?", pluralise_animal("I WANT THE ANIMAL NAME HERE, the `name` column's value"))
I want something similar to how you can use functions in MySQL while modifying column values. Is this out-of-scope or possible?
UPDATE animals SET plural_name = CONCAT(name, 's') -- just an example to explain what I mean by referencing a column. I'm aware of the problems in this example.
Thanks in advance
I cannot loop over the animal records for technical reasons.
Sorry, this cannot be done with this restriction.
If your pluralizing helper function is implemented in the client, then you have to fetch data values back to the client, pluralize them, and then post them back to the database.
If you want the UPDATE to run against a set of rows without fetching data values back to the client, then you must implement the pluralization logic in an SQL expression, or a stored function or something.
UPDATE statements run in the database engine. They cannot call functions in the client.
Use a ruby script to generate a SQL script that INSERTS the plural values into a temp table
File.open(filename, 'w') do |file|
file.puts "CREATE TEMPORARY TABLE pluralised_animals(id INT, plural varchar(50));"
file.puts "INSERT INTO pluralised_animals(id, plural) VALUES"
Animal.each.do |animal|
file.puts( "( #{animal.id}, #{pluralise_animal(animal.name)}),"
end
end
Note: replace the trailing comma(,) with a semicolon (;)
Then run the generated SQL script in the database to populate the temp table.
Finally run a SQL update statement in the database that joins the temp table to the main table...
UPDATE animals a
INNER JOIN pluralised_animals pa
ON a.id = pa.id
SET a.plural_name = pa.plural;

delete tag from XML in SQL 8

I have this XML table that is called xmlData and is in the saved_games db that keeps saves of users:
<customDataPostLoad>
<game_machine>
<_string>
RANDOM STRING DATA
</_string></game_machine></customDataPostLoad>
How can I delete the game_machine all together for all users ? I work with mysql workbench
trying something like this doesnt work:
Update saved_games
SET xmlData = (modify ('delete (/quest_machine)'))
update saved_games set xmlData=UpdateXML(xmlData, '/customDataPostLoad/game_machine', '')
Or perhaps
update saved_games set xmlData=UpdateXML(xmlData, '//game_machine', '')
The second parameter to UpdateXML is an xpath to search for; when exactly one match is found, the third parameter replaces it, and the possibly modified xml is returned.
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=6f112d151d425f3bbfe03af025468dcf

Replace multiple characters using MySQL

i am using this piece of code to replace characters in one column of my database.
UPDATE items
SET items = REPLACE(items, 'ḇ','ḇ')
But now I have a list with almost 500 characters to replace.
Just writing the whole sequence of lines in one single query will not work.
UPDATE items
SET items = REPLACE(items, 'ḇ','ḇ')
SET items = REPLACE(items, '&#x1E0x;','x')
SET items = REPLACE(items, '&#x1E0y;','y')
ETC.
Or I do not know how to write it.
Can anyone help me please?
Create a table that has the search string and the replace string as columns. Add all 500 rows of what needs to be replaced. Then write a stored procedure that will lookup the replace value from the lookup table and replace with the value. The lookup table can easily be loaded into MySql from an Excel or csv file.
Here's the pseudo code to show the looping and lookup. I know it won't compile, I'm a bit rusty on MySql syntax. I usually work in Oracle so the pseudocode syntax is more Oracle-esque.
DECLARE
v_old_string varchar;
v_new_string varchar;
BEGIN
FOR v IN (SELECT * FROM items) LOOP
SELECT old_string, new_string
INTO v_old_string, v_new_string
FROM my_lookup_table
WHERE old_string = v.thestringcolumn;
UPDATE items
SET itemcolumn = REPLACE(itemcolumn, v_old_string, v_new_string)
WHERE itemcolumn = v_old_string;
END LOOP;
END;

Getting sliced data between two timeskip database transfer data

How can I get 'sliced' data and remove the data from database?
I was using two database MySQL and SQL SERVER.
Example:
Yesterday I transfered data from MySql to Sql Server about 1000 rows,
then today I simply deleted 5 rows in MySql then did the transfer again.
So, how can I know which ID was deleted from MySQL then remove it in SQL Server?
I was transferring data using Stored Procedures that check every ID in every loop inserting.
foreach($data AS $key => $value){ $this->MsSQL->Exec('exec sp_pendaftar {ID} {NAME}'); }
I have stored procedure like this:
CREATE PROCEDURE [dbo].[sp_pendaftar] #id INT,#name varchar(45) AS DECLARE #id nvarchar(40); SET NOCOUNT ON; SET #id = (SELECT TOP 1 id FROM t_pendaftar WHERE id = #id);
IF #id IS NULL BEGIN
INSERT INTO t_pendaftar VALUES(#id,#name);
END;
ELSE
BEGIN
UPDATE t_pendaftar SET name = #name WHERE id = #id;
END;
GO
Please help.
I do not understand anything of the SQL-Server code, but I would suggest to make a part of the replication via application. So, for the bigger and more dynamical data tables you could define a delete_pendafter and a insert_pendafer table (and also a change_pendafter if needed).
Before you delete from t_pendafter, you select just those rows into delete_pendafter. This is even possible with triggers, if that does not slow down the application too much.
On SQL-Server-side I hope you have a delete-join, so you just remove the deleted rows. In MySQL this would look like
DELETE orig
FROM t_pendafter AS orig
INNER JOIN delete_pendafter AS del ON del.id = orig.id
This solution can be extended to INSERT and CHANGE, but must be done with some care.
Every now and then you should make a full copy, and you should write some checks to ensure the data is consistent.
Just got some answer from my partner.
First, grab array id from MySQL DB and then grab array id from SQL Server and compare which id that not present in SQL Server using array_diff().
$MySQL : array[id] => [11,12,13,15,16,19,25]
$SQL_Server : array[id] => [11,12,13,14,15,16,17,18,19,20,21,23,24,25 ]
$array_to_be_deleted : array($SQL_Server , $MySQL)
print_r($array_to_be_deleted);
result would be:
array => [14,17,18,20,21,23,24]
Hope anyone can try to correct me.

mysql update without replacing existing data

I need to update a row in my mysql database, but i don't want to replace the data that is already stored in it. example:
select books from storedb where id='Rick';
result of the query: Books = "example1"
but i need to update that row and add more books.
update storedb set books='example2' where id='Rick';
but it replaces the current data, so i need to do it without replacing current data.
somethink like this: books='example1 -- example2";
so you need a string concatenation?
Try this:
update storedb set books=CONCAT(books,' -- ', 'example2') where id='Rick';