MySQL: No update statements working - mysql

I'm trying to update a table of my database with values from another table and database, using a common field.
However none of my statements are working and I can't figure out why.
They are running for ten minutes (way too long) and then the server disconnects.
First try:
UPDATE database1.table1 t1, database2.table2 t2
SET t1.field1 = t2.field1
WHERE t1.field2 = t2.field2;
Second try:
UPDATE database1.table1 t1
INNER JOIN database2.table2 t2
ON t1.field2 = t2.field2
SET t1.field1 = t2.field1;
Can somebody push me in the right direction?
The WHERE/ON-conditions are working in separate select statements. As a workaround I created new tables with a SELECT JOIN, but that is very slow work.
field1 is unique in table2, but not table1. I want to update multiple entries in table1 with a unique value from table2.

Related

Mysql - updating and insert using select * using a target column

update table1 t1
inner join
table2 t2 on
t1.a = t2.a
set t1.b = t2.b,
t1.c = t2.c;
This code works to join 2 tables on column a. My problem is that I have about 500 columns which I want to update and am currently writing out each of the 500 columns in the code up to
t1.500 = t2.500;
This works, but it is slow and inefficient. Does anyone know how you can select * from table2 to update table1, keeping the join on t1.a = t2.a? All of the column names match exactly and am inserting all of the columns from table2. Was thinking of something like this below although I know that this is not correct. Thank you!
update table1 t1
inner join
table2 t2 on
t1.a = t2.a
set t1.* = t2.*;
I think there is no way to make update query with a wildcard in mysql. Don't know if it will properly fit to your problem, but you can try this workaround. :
DELETE FROM table2 WHERE id IN (<ids>);
Delete all the records from table2 that are satisfying given condition. And then insert the corresponding records from table1 to table2.
INSERT INTO table2
SELECT * FROM table1 WHERE id IN (<ids>);

SQL: insert only new records

This must be very trivial but I can't seem to find the solution.
I work with two tables, both without any primary key.
I want to add all the records of the first table to the second table only if they don't exist.
Basically:
INSERT INTO Table2
SELECT Table1.*
FROM Table
WHERE "the record to be added doesn't already exists in Table2"
You could do something like this. You would need to check each relevant column - I have just put in 2 as an example. With a Not Exists clause you can check if a record already existed across multiple columns. With a NOT IN you would only be able to check if a record already existed against one column.
INSERT INTO Table2
SELECT t1.*
FROM Table1 t1
WHERE NOT EXISTS
(
SELECT 1
FROM table2 t2 WHERE
t2.col1 = t1.col1 AND
t2.col2 = t1.col2
)
you could make usage of the EXISTS function:
INSERT INTO Table2
SELECT Table1.*
FROM Table1
WHERE NOT EXISTS(SELECT * FROM table2 WHERE <your expression to compare the two tables goes here>)
But i would advise you to check the use of unique index for your tables
Just an idea - untested:
INSERT INTO Table2
SELECT *
FROM Table1
WHERE NOT EXISTS(SELECT * FROM Table2 WHERE Table2.Field1 = Table1.Field1 AND Table2.Field2 = Table1.Field2)
You must add every Field of both Tables in the WHERE clause of the NOT EXISTS Query
INSERT INTO X.TableX1
(ColumX1,ColumnX2)
SELECT DISTINCT c.[ColumnY1],c.[ColumnY2]
FROM Y.Table2 c INNER JOIN Database.z.Table3 i ON c.ColumnX1= i.ColumnY1
WHERE NOT EXISTS
(
SELECT *
FROM X.TableX1 WHERE
X.TableX1.ColumnX1=c.ColumnY1
)
This is Joining two tables and filtering the data required and updating only the new values to third table on every run

Update statement in Mysql and Oracle

I have an update statement that is working in Mysql.(update multiple records, based on the column in another table)
UPDATE `table1` m
INNER JOIN
(SELECT cse_cd FROM table2 WHERE clsf_ind='NC') t
ON m.cse_cd = t.cse_cd
SET m.ST_CD = 'QUEST03'
However, it does not work in Oracle.
Can somebody help on this.
This should do the job:
UPDATE table1
SET ST_CD = 'QUEST03'
WHERE EXISTS
(SELECT 1
FROM table2
WHERE clsf_ind='NC' AND table1.cse_cd=table2.cse_cd);

MySql Query If Field equals Field in different table update different field

I have got two tables. I want to update MODEL in table2 when ITEM in table1 equals ITEM in table2.
Any Ideas?
In MySQL, you do it like this
UPDATE table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id
SET t1.col1 = t2.col1,
t1.col2 = t2.col2
If I understand correctly, you just want to perform an UPDATE on table2 based on, presumably, foreign keys?
If that's right, this should work:
UPDATE
table2
JOIN table1
ON table1.ITEM = table2.ITEM
SET
MODEL = 'new value';
The table declaration in an UPDATE statement is the same as is specified in a SELECT statement - so you can use any type of JOIN that fits your table/data.
Docs for UPDATE, SELECT.
If you could add an actual query attempt, or something, that might be helpful. Can you try something like the following:
UPDATE table2 JOIN table1 ON table2.ITEM = table1.ITEM SET MODEL = ?

MS Access How to convert this select query to Delete query?

I have the following select query
SELECT t1.*
FROM t1, t2
WHERE t1.field1=t2.field1 And t1.field2=t2.field2 And t1.field3=t2.field3 ;
I want to convert this into a delete query. how should i write it?
What about this query:
DELETE FROM t1
WHERE t1.field1 IN (
SELECT t1.field1 FROM t1, t2
WHERE t1.field1=t2.field1 And
t1.field2=t2.field2 And
t1.field3=t2.field3)
Not 100% on access but does:
DELETE t1
FROM t1, t2
WHERE t1.field1=t2.field1 And t1.field2=t2.field2 And t1.field3=t2.field3 ;
Work?
Try this
DELETE FROM t1
FROM t1 AS tt1, t2 AS tt2
WHERE tt1.field1=tt2.field1 And tt1.field2=tt2.field2 And tt1.field3=tt2.field3 ;
EDIT:
Did this in MS Access
DELETE DISTINCTROW t1.*
FROM t1 INNER JOIN t2 ON (t1.field3 = t2.field3) AND (t1.field2 = t2.field2) AND (t1.field1 = t2.field1);
And it worked, you have to set the Unique Records to Yes
What about this:
DELETE
FROM t1
INNER JOIN t2 ON t1.field1=t2.field1
And t1.field2=t2.field2
And t1.field3=t2.field3
Will delete all records in t1 that have a matching record in t2 based on the three field values.
I've been struggling with something similar.
I found the easiest way is not to use a query at all, but to create an empty duplicate table with multiple Primary Keys with Duplicates OK set (Design View hold down Ctrl key and select the rows you want and then right click and select them all as Primary Keys).
Then copy and paste all the rows from your table into the new table. OK the error messages and you will find you have a table with only unique values in the fields you wanted.
That has the additional benefit of not allowing duplicate rows in your new table.