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)
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 table1 and table2. I want to delete from table1 based on a condition in table2.
I have the following mysql query:
DELETE FROM table1
INNER JOIN table2 ON table2.col1 = table1.col1
WHERE table2.col2 = '1'
This return a syntax error. Is there something wrong with the above syntax?
You need to specify the table you are deleting from:
DELETE table1
FROM table1 INNER JOIN
table2
USING (col1)
WHERE table2.col2 = '1';
Try this:
DELETE FROM table1
WHERE EXISTS(
SELECT 'C'
FROM table2
WHERE table2.col1 = table1.col1
AND table2.col2 = '1'
)
You could do something like:
DELETE FROM table1 WHERE col1 IN (select col1 from table2 WHERE table2.col2 = '1');
I'm trying to update several rows with email addresses. T1.email is to be updated with T2.email based on T1.name existing in T2.name.
Currently, the query looks like this.
UPDATE T1
SET T1.email = T2.email
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.name = T2.name
WHERE T1.name = T2.name
AND (Some conditions)
LIMIT 1398
A similiar question is asked here, but a syntax error is given.
SQL UPDATE SET one column to be equal to a value in a related table referenced by a different column?
I've also tried updating with ANY.
UPDATE Table1
SET Table1.email = ANY
(
SELECT Table2.email FROM Table2
WHERE Table1.accountid = 901234
AND Table2.pid = 123
AND Table2.email IS NOT NULL
)
WHERE Table1.name IN
(
SELECT Table2.name FROM Table2
WHERE Table1.accountid = 19574
AND Table2.pid = 123
AND Table2.email IS NOT NULL
)
LIMIT 1398
Currently, this returns an error "SQL Error (1242): Subquery returns more than 1 row".
May be the beginning of a copy and paste job!
As detailed under UPDATE Syntax, for MySQL you want:
UPDATE Table1 T1 JOIN Table2 T2 USING (name)
SET T1.email = T2.email
WHERE (Some conditions)
LIMIT 1398
I have used the following query to insert values in table
INSERT INTO `tbl1` SELECT * FROM tbl2
Over here tb1 is a temp table.
Now, I want something like this
UPDATE `my_table` SELECT * FROM tbl1
I know that the syntax for update is Update tbl SET cols = vals
But can we have something like the insert query above ?
Thanks.
You can doInsert with Select but not Update with Select. But still possible by using JOIN within UPDATE.
UPDATE table1 t1 JOIN table2 t2 ON t1.id = t2.id
SET t1.col1 = t2.col2, t1.col2 = t2.col2
You can join your tbl1 table with my_table using the multiple-table UPDATE syntax:
UPDATE my_table JOIN tbl1 ON ***join_condition***
SET my_table.foo = tbl1.bar, ...
You can do something like this:
update my_table join tbl1 on my_table.id = tbl1.id
set my_table.Vaal= tbl1.vaal
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