update data in one table with the values from another table - mysql

I have 2 tables, shows and show_photo
shows structure:
show_photo structure:
What i want to do is update show_photo.modified column with show.year
This is what i tried but nothing gets updated.
UPDATE show_photo t2
JOIN shows t1 ON t1.id = t2.show_id
SET t2.modified = t1.year;

Converting my comment to an answer:
Change the column type from TIMESTAMP to VARCHAR first, run the
UPDATE query; and again change the data type from VARCHAR to INT
Your current table has modified column set to TIMESTAMP. But, you're trying to update its values to integers, which will fail.

Related

MYSQL: Error Code 1054 on Update Statement

I am trying to Update a column (say c1) from a table (say t1) sitting in database (say d1) with column say (c2) from table (say t2) sitting in database (say d2). The columns used to join two tables are column c3 in table t1 in database d1 and column c4 in table t2 in database d2.
My script for this looks like
UPDATE d1.t1
SET c1 = d2.t2.c2
WHERE d1.t1.c3 = d2.t2.c4;
On executing this, I get an Error 1054 stating that "Unknown column d2.t2.c4 in WHERE clause".
My code is as below:
UPDATE igr_raw_db.`master_database-v3_truncated`
SET `Transaction Type` = igr_keys.`transaction_type`.`Transaction_Type_Final`
WHERE
igr_raw_db.`master_database-v3_truncated`.`document_type` = igr_keys.`transaction_type`.`Reg_Transaction_Type_Raw`;
Here, I have two databases - igr_raw_db and igr_keys.
igr_raw_db has column Transaction Type which I am trying to set to value as per column Transaction_Type_Final in table transaction_type in database igr_keys
and I want to match column document_type with column Reg_Transaction_Type_Raw of the two databases.
I have checked my columns in tables for which I am getting an error and the columns exists with exact same names.
Below is my igr_keys.transaction_type table
Sr No int YES
Reg_Transaction_Type_Raw varchar(255) YES
Transaction_Type_Processed varchar(255) YES
Transaction_Type_Final varchar(255) YES
Below is a snipped of my igr_raw_db.master_database-v3_truncated table
consideration varchar(255) YES
document_type varchar(255) YES
Not able to understand why I am getting Error 1054.
Help will be much appreciated.
Thanks.
You must use multiple-table UPDATE Syntax:
UPDATE d1.t1
JOIN d2.t2 ON d1.t1.c3 = d2.t2.c4
SET d1.t1.c1 = d2.t2.c1;
If more than one row in source table matches a row in destination table then indefinite row from all matched ones will be used for updating. You must avoid this ambiguity (except the case when all these matched rows stores the same value, of course).
Of course you may use correlated query:
UPDATE d1.t1
SET c1 = ( SELECT c1
FROM d2.t2
WHERE d1.t1.c3 = d2.t2.c4 );
but this variant will produce an error if more than one row matches (can be fixed with LIMIT 1) or if correlated query uses another copy of the table to be updated (can be fixed by nested subquery). Also in most cases it should be slower.

Sum up columns using group by and store as a new column (permanently) in MySQL

I have 50 columns in the MySQL table. I want to sum all these columns and make a new column into the same table (sum50).
This should be stored permanently into the MySQL table whenever I update or insert new data.
I know how to sum up while performing the query but it does not store into the table permanently.
CREATE TRIGGER `name`
AFTER INSERT ON `table`
FOR EACH ROW
UPDATE `table` SET `table`.`coulms` = `table`.`col1`+.... `table`.`col50`+
I am trying the above trigger, but not working.
Should I have a blank column inserted into the table and perform trigger? Which trigger would be correct?
Instead of the trigger, add a generated column to your table:
alter table tablename add(sum50 int generated always as (col1 + col2 + ...) stored);
See a simplified demo.

Unable to copy data from one table to a new table in MySQL due to a String - Date type clash

I've a date column in table1 filled with character data that looks as follows:
InDate:
2022015
2012015
2122014
2112014
2102014
2092014
I've converted that data to following format:
InDate:
02-02-2015
02-01-2015
02-12-2014
02-11-2014
02-10-2014
02-09-2014
by using the following command:
update table1
set InDate = DATE_FORMAT(str_to_date(InDate, '%d-%m-%Y'), '%d-%m-%Y');
But obviously this only changed the look of the string data in the InDate column. Now, this still didn't suffice my situation and I needed the column itself to be in Date type.
So, I created a new empty table called table2 with the same structure as table1 (except for InDate column, which I now declared as Date type) and tried copying the old data (which is having InDate in my required format (02-02-2015)).
INSERT INTO table2
SELECT *
FROM table1;
But I'm getting the following error message:
Error Code: 1292. Incorrect date value: '02-02-2015' for column 'InDate' at row 1
I need this column to be in Date type with the data intact. This table has millions of records that I need to work with. I really need to get around this error.
Please help!
This code block maybe help use :
Not update table1.
insert into table2
(indate)
select date_format(str_to_date(indate
,'%d-%m-%Y')
,'%d-%m-%Y')
from table1;
Please try this.
INSERT INTO table2(`InDate`)
SELECT CONCAT('\'',InDate,'\'')
FROM table1;
Hope this helps.

Auto-store datetime() value in select insert query mysql

need an advice, how to auto-store datetime value for my historyActivity table in select insert mysql query. This is an example:
INSERT INTO history_sequence(CODE, LAST_MOUNTH, LAST_VALUE) SELECT CODE, MOUNTH, VALUE FROM seq WHERE CODE = CODEVALUE
i just want to add datetime to see time when the data inserted. Need help please
You can do this in the MySQL table definition:
ALTER TABLE history_sequence ADD inserted TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
When records are inserted into the table table, the inserted column gets automatically populated with the current timestamp.

Mysql single column table --> insert in other table

I have two tables: t1 and t2
- t2 has only 1 column named stuff (60.000 entries).
- t1 has 15 columns, including stuff (empty). t1 has about 650.000 entries.
How can I import the data from t2.stuff in t1.stuff when I have nothing to match it against? (I just want to populate empty fields of t1.stuff with data from t2.stuff and don't care about matching ids or anything.)
The best case (i think) would be, that if I run this query about 11 times, all fields of t1.stuff would be populated, because no empty field in t1.stuff is left over.
Here is an example what the tables look like:
t1:
|__a___|_b_|_c_|stuff|...|
|___308|foo|bar|_____|baz|
|___312|foo|bar|_____|baz|
...
|655578|foo|bar|_____|baz|
t2:
|___stuff___|
|some_info_1|
|some_info_2|
...
|some_info_n|
Maybe there are multiple steps required...
UPDATE
Here is the SOLUTION I went with in case someone has a similar problem - All credits go to user nurdglaw for pointing me in the right direction. So here we go:
Add a new column to your table in question populated with autoincrementing numbers (I set alter table t1 auto_increment = 1 and temporary disabled autoincrementing on my primary key, to avoid an error with this code) ALTER TABLE t1 ADD COLUMN new_column INTEGER UNIQUE AUTO_INCREMENT;
Did the same thing for t2. If you don't already have a second table, you can do something like this:
CREATE TABLE t2 (id INTEGER PRIMARY KEY AUTO_INCREMENT,t2_data_column VARCHAR(255)); <-- adjust number to your needs
and import your data with:
LOAD DATA LOCAL INFILE 'path_on_your_server/data_file.csv'
INTO TABLE t2
LINES TERMINATED BY '\r\n' <-- adjust to your linebreak needs
(t2_data_column)
Now that you have something to match against, you can INNER JOIN t1 with t2 by doing the following: Add the data from t2 to t1
UPDATE t1 AS s
JOIN t2 AS t ON t.id=s.new_column
SET s.stuff=t.t2_data_column; <-- stuff was the column in t1 I wanted to import the data to.
Tidy up the mess
DROP TABLE t2;
ALTER TABLE t1 DROP COLUMN new_column;
Enable autoincrement on your primary key again and set it to the number you need for new rows, if you used one before.
That is it, you're done!
One further note: I decided to adjust my data offline and import the 650.000 entries needed with this method in one go, rather than doing it with only the 60.000 I put in the initial question. But you'll get the idea of doing it with any number of data and match it with whatever you need.
INSERT statements create new rows in your table.
You need an UPDATE on the already existing rows
An easy way to do that is using an extern scripting language
; here is a rebol example
; assumming you use the mysql library from softinnov
; and a_ is the name of the unique key to a row in t1
db: open mysql://user:pass#mysql
insert db {select * from t1}
t1rows: copy db
insert db {select * from t2}
t2rows: copy db
foreach row t1rows [
insert db [ {update t1 set t1.stuff = ? where t1.a_ = ?} t2rows/1/1 row/1]
either tail? next t2rows [
t2rows: head t2rows
] [
t2rows: next t2rows
]
]
sorry, I still have difficulties with the formatting and the variables in your example
Try this
INSERT INTO t1 (stuff)
SELECT DISTINCT stuff FROM t2
I hope it helps