Mysql Update table1 from table2 values with CASE - mysql

I have table A & Table B . I need to update TableA.custid , with values (1 - 8 ) so I can reflect TableB.names
To make it more clear
TableA.custid = 1 , when TableB.name = 'Allen Gray'
TableA.custid = 2 , when TableB.name = 'Alex Watt'
TableA and TableB got the same number of records. And the relation between the 2 would be TableA.id = TableB.id
What would be the syntax for this?

Update tableA as t1 inner join TableB as t2
on t1.id=t2.id
set t1.name=t2.name

Related

Batch SQL query to update a field from different table

Table t1 - Fields a, b
Table t2 - Fields a, c
(a is common field between two tables)
I have added the field b in t2 and want to populate data for the same using references in t1
This can be done using following queries (e.g. a = 100, 101, ...)
update t2 set b = (select b from t1 where a = 100 limit 1) where a = 100;
update t2 set b = (select b from t1 where a = 101 limit 1) where a = 101;
Is there way this can be batched?
Use join:
update t2 join
t1
on t2.a = t1.a
set t2.b = t1.b;
You could also use a correlated subquery:
update t2
set b = (select t1.b from t1 where t1.a = t2.a);
However, non-matching values would be set to NULL.

how can I update a table but needing to get the update value from another select of the same table and other one?

I have this problem:
I have tableA and tableB
tableB needs to get the tableA's ID in a certain field.
tableB and tableA have a common field 'email'
Ive tried with this
Update tableB SET tableB.reference = (Select a.id from tableA a, tableB b where a.email = b.email)
Unfortunately when I run the query it says that I cant specify target 'tableB' for updates in FROM clause.
Any idea how to solve this or run a query like this?
UPDATE tableA, tableB
SET tableB.id = tableA.id
WHERE tableA.email = tableB.email
Or another one:
UPDATE tableB
INNER JOIN tableA USING (email)
SET tableB.id = tableA.id
Your query is also possible, but need to fix it:
Update tableB SET tableB.id = (Select a.id from tableA a, tableB b where a.email = b.email)
Update tableB SET tableB.id = (Select a.id from tableA a, tableB b where a.email = b.email)
You're trying to update tableA when you should update tableB, and also you don't need tableB in your sub-select:
UPDATE tableB SET reference = (SELECT id FROM tableA WHERE email = tableB.email)

How to transfer data between tables, only where id does not exist

I need to copy a set of data from TableA into TableB, like so:
INSERT INTO TableB(id,field1,field2)
SELECT id,field1,field2 FROM TableA
The above will work well, however TableB might already contain some of the records which I need to copy, identified by the PK id.
Thus, how can I add a clause to only insert a record if that id value is not already in TableB? I know a WHERE clause can be added at the end of the INSERT statement, but I am unsure of how to apply it for each and every record.
You can take a look for 3 methods comparing NOT IN vs. NOT EXISTS vs. LEFT JOIN / IS NULL.
The best way to search for missing values in MySQL is using a LEFT
JOIN / IS NULL or NOT IN rather than NOT EXISTS.
You can use NOT EXISTS.
INSERT INTO TableB (id, field1, field2)
SELECT id, field1, field2
FROM TableA t1
WHERE NOT EXISTS (
SELECT *
FROM TableB t2
WHERE t1.id = t2.id
)
Also you can use LEFT JOIN.
INSERT INTO TableB (id, field1, field2)
SELECT id, field1, field2
FROM TableA t1
LEFT JOIN TableB t2 ON t1.id = t2.id
WHERE t2.id IS NULL
Also you can use NOT IN.
INSERT INTO TableB (id, field1, field2)
SELECT id, field1, field2
FROM TableA t1
WHERE t1.id NOT IN (
SELECT t2.id
FROM TableB t2
WHERE t1.id = t2.id
)
You can use left join as below
INSERT INTO TableB(id,field1,field2)
SELECT id,field1,field2 FROM TableA
left join TableB on TableB.id = TableA.id
where TableB.id is null
you might want a where clause with exists()
INSERT INTO TableB(id,field1,field2)
SELECT id,field1,field2 FROM TableA a
WHERE not exists (SELECT 1 FROM TableB b WHERE a.id = b.id)
INSERT INTO TableB(id,field1,field2)
SELECT A.id,A.field1,A.field2 FROM TableA WHERE NOT EXISTS
(SELECT B.ID FROM TABLEB WHERE B.ID = A.ID)
Try to use a left join:
INSERT INTO TableB(id,field1,field2)
SELECT id,field1,field2 FROM TableA left join TableB on tableA.id = tableb.id where tablea.id is null
Better to use set operator EXCEPT for best performance as below:
here it will take two sets of data the it goes for minus and gives output
INSERT INTO TableB(id,field1,field2) (
SELECT id,field1,field2 FROM TableA
except
SELECT id,field1,field2 FROM TableB )
INSERT INTO TableB(id,field1,field2)
SELECT T2.id,T2.field1,T2.field2 FROM TableA as T1
inner join TableB as T2 on T1.id <> T2.id

three tables with Inner Join

I have the following three tables...
Table1
IDA colB colC
111 a w
222 b w
333 c s
444 b g
Table2
IDB colB colC
11 w f
12 w r
13 s g
Table3
IDA IDB
111 11
222 12
333 13
444 14
What I need is to copy from table1 to table2 and I could use the following easy MySQL query to do that...
INSERT INTO table2 SELECT * FROM table1
The problem is I don't the same id type,...the two tables are connected over the third table table3.
in which IDA contains table1 primary key and IDB contain table2 primary key,
so, example if I want to copy from table1 IDA(111) to table2 how do I do that?
and if the IDB exists how do I update on Duplicate Key...
I have the following query but no working...
INSERT INTO table2 SELECT * FROM table1
WHERE IDA IN ( SELECT table1 b
INNER JOIN table3 c ON c.IDA = b.IDA
INNER JOIN table2 a ON a.IDB = c.IDB )
WHERE b.IDA=111
But, I wish if I get generalize answer...Thanks
INSERT INTO table2
SELECT
t3.idb
,t1.colb as ncolb
,t1.colc as ncolc
FROM
table1 t1
join table3 t3
on t1.ida = t3.ida
ON DUPLICATE KEY UPDATE
colb = ncolb
,colc = ncolc
No MySQL on me right now so syntax might not be 100% correct, but this should give you the idea of how it should be done.
Depending on whether table3 has entry for each table1 id you might need to change t3.idb to coalesce(t3.idb, t1.ida) and change join to left join in the query if you want them to be copied. Remember that table2 will then have ids from table1)
INSERT INTO table2 SELECT * FROM table1
WHERE IDA IN ( SELECT * FROM table1 b
INNER JOIN table3 c ON c.IDA = b.IDA
INNER JOIN table2 a ON a.IDB = c.IDB ) HAVING b.IDA=111

How to update a table using a select group by in a second one and itself as the data source in MySQL?

I can do this:
SELECT t2.value + sum(t3.value)
FROM tableA t2, tableB t3
WHERE t2.somekey = t3.somekey
GROUP BY t3.somekey
But how to do this?
UPDATE tableA t1
SET speed = (
SELECT t2.value + sum(t3.value)
FROM tableA t2, tableB t3
WHERE t2.somekey = t3.somekey
AND t1.somekey = t3.somekey
GROUP BY t3.somekey
)
;
MySQL says it's illegal since you can't specify target table t1 for update in FROM clause.
You can do it by rewriting your query:
UPDATE tableA t1, (
SELECT somekey, SUM(value) value
FROM tableB t3
GROUP BY somekey
) t2
SET speed = t1.value + t2.value
WHERE t1.somekey = t2.somekey;
You are using MySQL's extension to GROUP BY which should not be used in this context.
Given the following values:
tableA
value somekey
1 1
2 1
tableB
value somekey
3 1
4 1
this subquery:
SELECT t2.value + SUM(t3.value)
FROM tableA t2, tableB t3
WHERE t2.somekey = t3.somekey
AND t1.somekey = t3.somekey
GROUP BY
t3.somekey
will return you either 8 or 9: it will calculate SUM(b.value) = 7 and then add a random value from A for the given key.
For this sample data, which value do you want the speed to be updated to?