Trying to create a mysql query that will do the following:
For each record within Table 1, find a matching record in Table 2 where column A & B of Table 1 match column Y & Z of Table 2.
Once a match is found, grab column X value from Table 2 record and insert that value into column C of the original record in Table 1.
I hope that makes sense.
How the heck do I do that?
Use a multi-table update, just without modifying any columns from Table 2, as follows:
UPDATE Table1 T1, Table2 T2
SET T1.C = T2.X
WHERE T1.A = T2.Y AND T1.B = T2.Z;
UPDATE table1 INNER JOIN table2 ON table1.a = table2.y AND table1.b = table2.z SET table1.c = table2.x;
Related
I have a pretty simple MySQL query to implement, but I can't figure out how...
I have two tables, T1 and T2.
What I need to do:
From T1, I retrieve an ID based on a CODE value:
SELECT id FROM T1 WHERE code = '$code';
Then I need to use this ID (so the value I just retrieved) to update a specific row in T2 (the name of the row will match the ID's value).
I was thinking about using either subqueries or user-defined variables, but no matter how I try it I can't get it done.
If you have any code snippet that can help me doing that, I would appreciate it as well!
EDIT
Just to clarify something: I don't know the name of the column that I need to update in T2, since that name will be the value I retrieve from T1.
So for example, if the ID I get from T1 is "03", it will update the column named "03" in T2.
EDIT 2
Here's a little schema of what I intend to achieve (hoping I make myself clearer, I'm sorry for the misunderstanding...)
UPDATE T2 SET COL = YOUR_VALUE
WHERE T2.ID = (SELECT id FROM T1 WHERE code = '$code')
UPDATE: If the sub query returns more than one row then you can use from IN operator
UPDATE T2 SET COL = YOUR_VALUE
WHERE T2.ID IN (SELECT id FROM T1 WHERE code = '$code')
Use an UPDATE with a JOIN:
UPDATE T2
CROSS JOIN T1
SET T2.`0` = IF(T1.id = 0, T1.someColumn, T2.`0`),
T2.`1` = IF(T1.id = 1, T1.someColumn, T2.`1`),
T2.`2` = IF(T1.id = 1, T1.someColumn, T2.`1`)
WHERE T1.code = '$code'
Replace someColumn with the column in T1 containing the value you want to put into T2.
you can update without using the subquery just using join
update t2
inner join t1 on t2.name = t1.id and t1.code ='$code'
set t2.my_col = 'my_value'
but you should not use var in your query you are at risk for sql injection take a look at you mysql driver for param_binding
UPDATE T2 SET COL = YOUR_VALUE WHERE EXISTS
(SELECT 1 FROM T1 WHERE T2.id=T2.id AND code = '$code')
You can update T2 with the next SQL if you expect at least one row from the inner query
UPDATE T2 SET COLUMN = VALUE
WHERE T2.ID IN (SELECT ID FROM T1 WHERE CODE = $code)
Otherwise, if from T1 you are sure you will get only 1 record
UPDATE T2 SET COLUMN = VALUE
WHERE T2.ID = (SELECT ID FROM T1 WHERE CODE = $code)
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>);
Need help with a mysql query. I have 2 tables - table1 and table2. I am trying to update a field in table1 which is not included in table2 - named Status. And I want to update that field with the value of 'A'. table1 and table2 DO have a field in common - named Member_ID. Here is my query that is giving me errors:
UPDATE table1 SET Status='A' WHERE Member_ID=table2.Member_ID;
Is there some type of join that is needed? Any help would be appreciated. Thanks.
cdr6800
This can be done with exists
UPDATE table1 s
SET s.status = 'A'
WHERE EXISTS(select 1 from table2 t
WHERE t.member_id = s.member_id)
You have to JOIN table1 to table2 like this:
UPDATE table1 AS t1
INNER JOIN table2 AS t2 ON t1.Member_ID = t2.Member_ID
SET t1.Status='A'
This will update all table1 records having a Member_ID value that also exists in table2.
I have two almost identical database tables with the presice same colummns and number of rows in them.
They both have a unique ID per row which is the same in both tables.
The first table is missing some values on some columns.
How can i copy values from a row in table2, to row in table1?
The columns have the same name, but the value is empty in table1, but not table2. I want to copy over all values from some columns in table2 to same columns in table1.
It's a big table with over 1 million rows.
Example:
Table 1 Row 5:
id = 5
orgnr = 932942
homepage = NULL
name = NULL
Table 2 Row 5:
id = 5
orgnr = 932942
homepage = 'www.domain.com'
name = 'John Smith'
I want to copy values from homepage and name to table1's columns.
Thanks in advance.
In MySQL you can use joins to update a table with values from another table:
UPDATE table1 t1
JOIN table2 t2
ON t1.id = t2.id
SET t1.homepage = t2.homepage,
t1.name = t2.name
Dirty custom query that requires you to add all fields but could do the trick:
UPDATE table1
SET
field1 = ISNULL(t1.field1, t2.field1),
field2 = ....
FROM
table1 t1
INNER JOIN table2 t2 ON t1.Id = t2.Id
If updating the 1m rows in one go is too much you can try to do it in batches by using a where clase:
WHERE
t1.Id BETWEEN #batchStart AND #batchEnd
I have
TABLE1 with COLUMNS A,B,... and want to check if values of COLUMN A are in some other list e.g. column X in TABLE2. The result should be column C in TABLE 1 (Boolean TRUE, FALSE)
Is there some elegant way how to do this? What I am doing now is SELECT +LEFT JOIN into a new table(CREATE TABLE X SELECT...). Is there some possibility to do UPDATE TABLE1 SET C =.... So no other table but only the old TABLE1.
thank you
Try this:
UPDATE Table1 t1
LEFT JOIN Table2 t2 ON t2.X = t1.A
SET t1.C = (CASE WHEN t2.X IS NOT NULL THEN TRUE ELSE FALSE END);