need help building a query to replace fields on a table that match a second table.
table1
city
table2
city | code
I need replace all occurrences of city on table1 by code of the table2 matching the city field
i think it'll be something like this:
update table1 t1 set t1.city = t2.code from table2 t2 where t1.city =
t2.city
Actually, not necessary for a replace:
UPDATE table1 JOIN table2 ON table1.city = table2.city SET table1.code = table2.code
Related
I got 2 tables in same database in MYSQL and I want insert the columnA in table1 to columnA in table2 and my condition is if table1.name is equal to table2.name. I tried this but didnt work
INSERT INTO Table2 (solId, openTime, closingTime, guid)
SELECT solId, openTime, closingTime, guid
FROM Table1
WHERE Table2.name = Table1.name;
You may want an update:
update table2 t2 join
table1 t1
on t1.name = t2.name
set t2.solId = t1.solId,
t2.openTime = t1.openTime,
t2.closingTime = t1.closingTime,
t2.guid = t1.guid;
I have two tables bound through an ID field:
table1: id, name, type
table2: id, id_table1, date, status
I have to collect all the records of the table1 that have a certain value of type field and that are not been referenced in table2 plus all the records of table1 referenced in table2 that have a certain status field value.
For the first part if I remember correctly I can use the LEFT JOIN command:
LEFT JOIN table1.name
LEFT JOIN table2
ON table2.id_table1 = table1.id
WHERE (table1.value = 'value1') AND (table2.id_table1 IS NULL);
but for the second part I'm getting lost...
I'm using MySQL 5.6 and I would like to define a View to handle this.
SELECT t1.*, t2.*
FROM table1 t1
LEFT JOIN table2 t2
ON table2.id_table1 = table1.id
WHERE (t1.type= 'value1' AND t2.id IS NULL)
OR (t2.status = 'certain status' )
I would think you could just change the WHERE to:
WHERE (table1.value = 'value1')
AND (table2.id_table1 IS NULL
OR
([the other table2 status criteria)
)
;
You can try this...
SELECT T1.*,T2.*
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T1.Value = 'value1' AND T2.id_table1 IS NULL
UNION
SELECT T1.*,T2.*
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T2.Status= 'Status Criteria'
let's say I have select, which return me from table1:
ID Name
1 Bob
2 Alice
3 Joe
Then I want UPDATE values in another table based on this result:
UPDATE table2 SET Name = table1.Name WHERE ID = table1.ID
As I understood, I can only do internal select in one place, like:
UPDATE table2 SET Name = (select Name from table1) WHERE ...
And I don't know how to specify WHERE-condition.
all you should do is just join the tables like this.
UPDATE table2 t2
JOIN table1 t1 ON t1.id = t2.id
SET t2.name = t1.name;
RESULTS WITH JOIN
if you are set on doing it with a select you could do it like this.
UPDATE table2 t2,
( SELECT Name, id
FROM table1
) t1
SET t2.name = t1.name
WHERE t1.id = t2.id
RESULTS FROM SELECT
UPDATE table2
SET name = (SELECT table1.Name FROM table1 WHERE table1.id = table2.id)
WHERE apply_condition
EDIT:#1
UPDATE table2 t2, (SELECT id, name FROM table1) t1 SET t2.name = t1.name WHERE t1.id = t2.id
please read this link,another
Try this
Update table2
Set Name = (Select Name From table1 where table1.ID = table2.ID)
Where table2.ID In (Select ID From table1)
Case:
How to update table1 with data from table2 where id is equal?
Problem:
When I run the following update statement, it updates all the records in table1 (even where the id field in table1 does not exist in table2).
How can I use the the multiple update table syntax, to update ONLY the records in table1 ONLY where the id is present in table2 and equal?
UPDATE table1,table2
SET table1.value=table2.value
WHERE table2.id=table1.id
Thanks in advance.
here's the correct syntax of UPDATE with join in MySQL
UPDATE table1 a
INNER JOIN table2 b
ON a.ID = b.ID
SET a.value = b.value
SQLFiddle Demo
EDIT
For MySql it'll be
UPDATE table1 t1 INNER JOIN
table2 t2 ON t2.id = t1.id
SET t1.value = t2.value
sqlfiddle
Original answer was for SQL Server
UPDATE table1
SET table1.value = table2.value
FROM table1 INNER JOIN
table2 ON table2.id=table1.id
sqlfiddle
You can try this:
UPDATE TABLE1
SET column_name = TABLE2.column_name
FROM TABLE1, TABLE2
WHERE TABLE1.id = TABLE2.id
UPDATE table1
SET table1.value = (select table2.value
WHERE table2.id=table1.id)
I want to join table1 with table2 on column 'Name', but table2.Name has an 'e' in front of all the names (if table1.name=ABC,table2.name=eABC). How am I supposed to use a join for those two?
I tried FROM table1 join table2 on 'e'+table1.name = table2.name, but it doesn't work...
SELECT *
FROM table1 t1
JOIN table2 t2
ON t2.name = CONCAT('e', t1.name)
Try using a substring of the table2 name. So something like:
SELECT *
FROM table1
, table2
WHERE table1.name = substring(table2.name, 1, length(table2.name))
I can't remember if substring is zero based, so just play with the numbers.