i need some help to get a fast update on my table in MySQL
Table 1
id | value
1 0
2 0
3 0 ...
Table 2
t1_id | t2_id
1 2
1 3
3 5 ...
Have about 150,000 rows in table 1, and about 1,3 million in table 2. I need set t1.value = 1 when t1.id exists in table 2.
update table1 t1, table2 t2
set value = 1
where t1.id = t2.id;
Without some distinct parameter, it will do many times for each id, making it slow to update all t1 rows.
Any help would be gladly accepted.
what about:
UPDATE t1
SET t1.value = 1
FROM table_t1 t1
WHERE EXISTS (SELECT 1
FROM table_t2 t2
WHERE t2.id = t1.id
)
what about:
update table1
set value=1
from table2
where table1.id=table2.t1_id
Related
I'm not sure if I write my questions title correctly, but my example would probably explain it better.
Join table_1 and table_2 and get table_1 row, if there is no is_validated= 1.
The query will get table_1, id 1 and 3, because they dont have is_validated = 1
table_1
id
1
2
3
table_2
id table_1_id is_validated
1 1 0
2 2 1
3 2 0
4 3 0
5 3 0
expected result
id
1
3
I've try to use this query, but it doesn't get the result that I want.
SELECT * FROM table_1 t1
JOIN table_2 t2 ON t1.id=t2.table_1_id AND t2.is_validated = 0
GROUP BY t2.table_1_id
Exists logic works nicely here:
SELECT t1.id
FROM table_1 t1
WHERE NOT EXISTS (SELECT 1 FROM table_2 t2
WHERE t2.table_1_id = t1.id AND t2.is_validated = 1);
I have a correlated subquery that is validated based on the column value. Even though I read through the sequelize docs and examples on GitHub, I did not really get any working solutions.
Query I am trying to execute
select count(*) from table1 t1
where case when deciding_col=1 then t1.id in
(select distinct t2.id from table2 t2 where t2.id=t1.id and t2.second_condition=132)
else 1=1 end;
Any references or suggestions are appreciated.
Update:
The goal is to fetch all records from table1, but if deciding_col value is true for a record, then I need to check if that particular record exist in table2.
Example:
Table1 Data:
id | name | deciding_col
1 abcd 1
2 edfg 0
3 xyz 0
Table2 Data:
id | second_condition
1 132
Output:
1.
select t1.* from table1 t1
where case when deciding_col=1 then t1.id in
(select distinct t2.id from table2 t2 where t2.id=t1.id and t2.second_condition=132)
else 1=1 end;
id | name | deciding_col
1 abcd 1
2 edfg 0
3 xyz 0
2.
select t1.* from table1 t1
where case when deciding_col=1 then t1.id in
(select distinct t2.id from table2 t2 where t2.id=t1.id and t2.second_condition=133)
else 1=1 end;
id | name | deciding_col
2 edfg 0
3 xyz 0
I have 3 tables as follows
Table1
Id Name
1 abcd
2 bcd
3 dabc
Table2
Id2 Name2
2 xyz
3 def
4 mno
Table3
Id Id2 Value
1 4 1
2 3 1
3 4 1
Now,
From table1 : I have to select all Id where Name is %abc%
From table2: I have to select Id2 where Name2 is "mno"
From Table3: I have to change value to 0 from 1 where Id's value are from Table1 and Id2 is from Table2.
Table 1:
select Id from Table1 where Name like '%abc%'
Table2 :
select Id2 from Table2 where Name2 = "mno"
Table 3:
update Table3 set Value = 0 where Id in() and Id2=
But, I dont know how to make it 1 single query. Can anyone please guide me up ?
Refer to: prior stack article
You've not explained how T1 relates to T2, So I have assumed a cross join.
Whenever you have a record in T1 with name like '%abc%' (1,3) in your data..
and whenever you have a record in T2 with a name equal to 'mno' 4 then you want the value in table 3 to be 0
so the select we generate should produce
1,4
3,4
and when we inner join this back to table 3 it only selects
Id Id2 Value
1 4 1
3 4 1
Now we generate an update based on this select as outlined in the link provided above...
UPDATE table3
INNER JOIN (
SSELECT t1.ID t1ID, t2.id t2ID
FROM table1 t1
CROSS JOIN table2
WHERE t1.name like '%abc%'
and t2.name like = 'mno') B
on B.t1ID = t3.Id
and B.t2ID = T3.ID2
SET value = 0
Giving us a result of
Id Id2 Value
1 4 0
2 3 1
3 4 0
if we select * from table3
update t3
set t3.Value = 0
from Table3 t3
inner join Table1 t1
on t3.Id = t1.Id
inner join Table2 t2
on t3.Id2 = t2.Id2
where t1.Name like '%abc%' and t2.Name2 = 'mno'
OR
update Table3
set value = 0
where Id in (select Id from Table1 where Name like '%abc%')
and Id2 in (select Id2 from Table2 where Name2 = 'mno')
You should think about UPDATE ... WHERE EXISTS as follows:
update Table3 set Value = 0
WHERE EXISTS (SELECT 1 FROM Table1 where Name LIKE '%abc%' AND Table1.Id=Table3.Id )
AND EXISTS (SELECT 1 FROM Table2 where Name2 = "mno" AND Table2.Id2=Table3.Id2)
I have a table:
Table1:
Id value
1 10
2 20
3 30
4 40
In table 2:
Id
1
1
2
2
3
4
4
4
I want to get the results:
Table2:
Id value
1 10
1 10
2 20
2 20
3 30
4 40
4 40
4 40
I know add a column with following code:
ALTER table Table2 ADD value int(11)
How to fill the data??
I think these SQL useful to you.
UPDATE Table2 LEFT JOIN Table1 ON Table2.id = Table1.id
SET Table2.value = Table1.value
Thank you.
you need to first ALTER the table and then fill the values using JOIN command as:
ALTER table Table2 ADD value int(11);
Then insert / fill the values as:
UPDATE Table2 INNER JOIN Table1
ON Table2.id = Table1.id
SET Table2.value = Table1.value
You could run this update command juste after tours.
UPDATE Table2 SET value = (SELECT value FROM Table1 WHERE Table2.Id = Table1.Id)
Given the following table structures:
T1:
id a
1 1
2 2
3 3
T2:
id t1_id b
1 1 1
2 1 2
3 2 3
I need to add the value of t2.b to the value of t1.a where t2.t1_id = t1.id.
A simple update with a join like the following isn't working:
UPDATE t1
JOIN t2
ON t2.t1_id = t1.id
SET t1.a = t1.a + t2.b
WHERE t2.id IN(1,2)
Expected T1 result (adds 1 and 2 to t1.a = 1; 3 to t1.a = 2):
id a
1 4
2 5
3 3
Actual T1 result (only adds 1 to t1.a = 1; 3 to t1.a = 2):
id a
1 2
2 5
3 3
At the moment I'm looking at a select that computes the full sum of values to be added using a group by, and then that result set is joined to the update... This seems like overkill for something so simple though! Does anyone have an elegant and efficient solution?
Came up with this real quickly.
UPDATE T1 AS t
JOIN (
SELECT
t1_id, SUM(b) AS sum_total
FROM
T2
WHERE
t1_id IN (1, 2)
GROUP BY
T2.t1_id
) AS t2 ON t.id = t2.t1_id
SET t.a = t.a + t2.sum_total;