Table t1 - Fields a, b
Table t2 - Fields a, c
(a is common field between two tables)
I have added the field b in t2 and want to populate data for the same using references in t1
This can be done using following queries (e.g. a = 100, 101, ...)
update t2 set b = (select b from t1 where a = 100 limit 1) where a = 100;
update t2 set b = (select b from t1 where a = 101 limit 1) where a = 101;
Is there way this can be batched?
Use join:
update t2 join
t1
on t2.a = t1.a
set t2.b = t1.b;
You could also use a correlated subquery:
update t2
set b = (select t1.b from t1 where t1.a = t2.a);
However, non-matching values would be set to NULL.
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;
When I run this update query in my server. Desired records are updating according to requirement. But all the other records reside in table1 set as NULL expect newly updated one. Any one there who can help on this. Thankyou in advance.
UPDATE MYDB1.`table1` t1
JOIN MYDB1.`table2` t2
ON t2.id = t1.table2_fill_id
JOIN MYDB2.tbl3 t3
ON t2.abc_id = t3.abc_id AND
t2.date = t3.opn_date AND
t2.flag IS TRUE
SET t1.value = t3.column15_value
WHERE code = "ABCD";
first set output for last tables like
update table4 d join
(select a.id, b.name from table1 a, table2 b where a.id = b.id) table3
on d.id = table3.id and d.code = 'abcd'
set d.name = table3.name
this is just an example
use your tables
Here's my questions in writing some scripts in MySQL:
I get a table T1 with some columns called id, t1_col_01, t1_col_02, and a table T2 with some columns called id, t2_col_01, t2_col_02.
For each row R1 in T1, I want to update R1.t1_col_01 = 'Yes' if the there are multiple rows in T2 that has the same id column with R1.id. If not, set R1.t1_col_01 = 'No'.
I tried to write:
update T1, T2
set
T1.t1_col_01 = 'Yes'
where
(select count(*) from T2 where T2.id = T1.id) > 1
But it didn't work.
What you need is this:
update T1
inner join T2
on ( T2.id = T1.id )
set T1.t1_col_01 = 'Yes'
where (select count(*) from T2 where T2.id = T1.id) > 1
See it here on fiddle:
http://sqlfiddle.com/#!2/0edc4/1
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'
I can do this:
SELECT t2.value + sum(t3.value)
FROM tableA t2, tableB t3
WHERE t2.somekey = t3.somekey
GROUP BY t3.somekey
But how to do this?
UPDATE tableA t1
SET speed = (
SELECT t2.value + sum(t3.value)
FROM tableA t2, tableB t3
WHERE t2.somekey = t3.somekey
AND t1.somekey = t3.somekey
GROUP BY t3.somekey
)
;
MySQL says it's illegal since you can't specify target table t1 for update in FROM clause.
You can do it by rewriting your query:
UPDATE tableA t1, (
SELECT somekey, SUM(value) value
FROM tableB t3
GROUP BY somekey
) t2
SET speed = t1.value + t2.value
WHERE t1.somekey = t2.somekey;
You are using MySQL's extension to GROUP BY which should not be used in this context.
Given the following values:
tableA
value somekey
1 1
2 1
tableB
value somekey
3 1
4 1
this subquery:
SELECT t2.value + SUM(t3.value)
FROM tableA t2, tableB t3
WHERE t2.somekey = t3.somekey
AND t1.somekey = t3.somekey
GROUP BY
t3.somekey
will return you either 8 or 9: it will calculate SUM(b.value) = 7 and then add a random value from A for the given key.
For this sample data, which value do you want the speed to be updated to?