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;
Related
I am trying to update the table T1 by counting the results of a SQL Query from other tables T2 and T3.
This is the query I came up with but it does not seem to work:
UPDATE T1
set Stock =
(SELECT count(ID_Item)
FROM T2,T3
WHERE T2.ID_Product = T1.ParentSKU AND
T3.ID = T2.ID_Product AND
Stock_Items.Name = '' AND
Stock_Items.Returned = ''
GROUP BY(T3.Size)
)
What I am trying to do is to update T1 by counting the results from T2 and grouping distinct sizes from T3.
You need to remove the GROUP BY, if your item has more than one size your subquery will return multiple results, and the update will fail.
UPDATE T1
set Stock =
(SELECT count(ID_Item)
FROM T2 INNER JOIN T3
WHERE T2.ID_Product = T1.ParentSKU AND
T3.ID = T2.ID_Product AND
Stock_Items.Name = '' AND
Stock_Items.Returned = '' AND
T3.Size = T1.Size
)
if Size is not a factor, then remove it completely from the subquery
I was trying to optimize this join since both tables are large (# rows, # records):
Update Table1 as T1
Inner Join Table2 as T2
On T1.X=T2.Y
Set T1.A=T2.B;
Ended up getting great performance increase doing:
Update Table1 as T1
Inner Join (Select T2.Y,T2.B from Table2) as T2
On T1.X=T2.Y
Set T1.A=T2.B;
So figured I'd do the same for Table1. Yet when I try:
Update (Select T1.X, T1.A from Table1) as T1
Inner Join (Select T2.Y,T2.B from Table2 )as T2
On T1.X=T2.Y
Set T1.A=T2.B;
I get the error that T1 is not updateable. How can I know limit the fields loaded by Table1 as well?
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
Drop the ORDER BY + LIMIT, or the JOIN, and everything is peaches. Put them together and I seem to release the Kraken. Anyone who can shed some light?
DELETE table1 AS t1
FROM table1 AS t1 LEFT JOIN table2 AS t2 ON t1.id = t2.id
WHERE t2.field = 'something'
ORDER BY t1.id DESC
LIMIT 5
(Delete using aliases)
I've also tried it without aliases & dropping the WHERE, to no avail. Always a syntax error "near 'ORDER BY...".
From Mysql Docs: DELETE
For the multiple-table syntax, DELETE deletes from each tbl_name the rows that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used.
In your case, I think this works:
DELETE
FROM table1
WHERE EXISTS
( SELECT t2.id
FROM table2 AS t2
WHERE t2.id = table1.id
AND t2.field = 'something'
)
ORDER BY id DESC
LIMIT 5
If you really need to do this you can do the following
DELETE table1
WHERE id in
(SELECT t.id
FROM table1 AS t INNER JOIN table2 AS t2 ON t1.id = t2.id
WHERE t2.field = 'something' --No point in doing a LEFT JOIN because of this
ORDER BY t1.id DESC
LIMIT 5)
t1 was not declared as an alias. Try t everywhere you have t1 (or vice versa).