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
Related
Update table2 t2
set t2.c1=t1.c1
inner join table1 t1 on ( t1.c2 = t2.c2)
where t2.c1 = "-1";
I want to execute the above query which will update the table2 column from table1 column ON INNER JOIN matching conditions. It is working fine. I am running a migration where the rows count are in million in both tables. I thought of limiting the update query in batches for query optimization but limit is not allowed in update query.
I can try with select query with limit option, but updating multiple columns would not work with this below query.
update table2 t2
set t2.c1=<?>
where t1.c2 = ( select c2 from table);
Can anyone help to use update query with optimization? Will updating millions row have any impact?
You could move the limiting clause to the joined table, like so:
update table2 t2
inner join (
select c1, c2
from table1
order by c2
limit ?, ?
) t1 on t1.c2 = t2.c2
set t2.c1 = t1.c1
where t2.c1 = -1
I am not sure what you really want to do, but you can update multiple columns with update and limit.
The following is fine:
update table2 t2
set t2.c1 = <?>,
t2.c3 = ?
where t1.c2 = ( select c2 from table)
limit 100;
So I have two tables. T1 is completely filled and looks like this:
ean | id | title | price |
T2 looks the same, but has missing records. It contains the id's of T1. So what I wanna do, is fill in the empty records with T1's data on T1.id = T2.id.
I looked up my question on stackoverflow and came up with this code:
UPDATE monitoren
SET T2.ean = T1.ean
FROM T2
JOIN T1
ON T1.id = T2.id;
(Of course I also wanna update the columns title and price in T2.)
This gives me a syntax error at 'FROM T1 JOIN T2 ON T1.id = T2.id'
MySQL does not support a FROM clause. Instead, you express the join like this:
UPDATE T2 JOIN
T1
ON T1.id = T2.id
SET T2.ean = T1.ean ;
I am trying to delete rows from a table that spit out from a join:
DELETE FROM t1 WHERE company_name IN
(SELECT company_name FROM t1
LEFT OUTER JOIN t2
ON t2.company_name = t1.company_name
WHERE t2.name IS null)
Column 'company_name' in field list is ambiguous
Getting this ambiguous error while trying to make this query? Any suggestions?
MySQL doesn't like it when you try to UPDATE/DELETE a table and SELECT from the same table in the same query.
You can solve this with multi-table DELETE syntax:
DELETE t1 FROM t1 LEFT OUTER JOIN t2 USING (company_name)
WHERE t2.name IS NULL;
As the error message tells the column name company_name is not unique. Depending on your needs I believe this may solve your problem assuming you're trying to delete entries in t1 which doesn't have a corresponding row in t2 or have one but with name is null:
DELETE FROM
t1
WHERE
company_name NOT IN (
SELECT
t2.company_name
FROM
t2
WHERE
t2.name IS NOT NULL
)
Try that
DELETE FROM t1 WHERE company_name IN ( select * from (SELECT t1.company_name FROM t1 LEFT OUTER JOIN t2 ON t2.company_name = t1.company_name WHERE t2.name IS null) t )
Ok. I have some data in one table, that references on multiple occasions some data in another table.
Table1 - main client table
Table2 - user defined fields
Say I have a query that shows a client id from Table1 and all attached / used "used defined fields" from Table2
SELECT t1.Id, t2.udf
FROM Table1 t1
JOIN Table2 t2 ON t1.Id = t2.Index
WHERE t1.EndDate IS NULL AND
t1.Id = '1234.9876' AND
I would get the following for a result...
ID UDF
1234.9876 100
1234.9876 110
1234.9876 118
1234.9876 124
1234.9876 198
1234.9876 256
Now, say I wanted to query this same thing, and get ONLY the ID of the Client, but ONLY IF a value for t2.udf equaling '194' did not exist. So, I would simply get
ID
1234.9876
...as a result.
Make the join a LEFT join and filer where t2.Index is null
SELECT t1.Id
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.Id = t2.Index
AND t2.UDF = 194 -- has to be before where clause
WHERE t2.Index IS NULL
AND t1.EndDate IS NULL
AND t1.Id = '1234.9876' -- not sure if you want this part
Another way by using NOT EXISTS
SELECT t1.Id
FROM Table1 t1
WHERE NOT EXISTS (SELECT 1 FROM Table2 t2 WHERE t1.Id = t2.INDEX
AND t2.UDF = 194)
AND t1.EndDate IS NULL
AND t1.Id = '1234.9876'
See also JOINS
You can add AND t2.udf not in (select udf from table2 where udf <> '194').
But #SQLMenace solution is better
This should do it.
SELECT DISTINCT t1.Id
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.Id = t2.Index
WHERE t2.UDF NOT IN (194)
AND t2.Index IS NULL
Select DISTINCT gives you unique entries that satisfy the other conditions, and the first where clause
t2.UDF NOT IN (194)
Normall would return all the rows for the t1 where the t2.UDF is not 194, but it is limited by the Select Distinct to give you only distinct id's
Try the following:
SELECT t1.Id
FROM Table1 t1
JOIN Table2 t2 ON t1.Id = t2.Index
WHERE t1.EndDate IS NULL AND
t1.Id = '1234.9876' AND
t2.udf <> '194'
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)