I'm trying to figure out how to mass update a mysql table based on if a value exists in a column in another table.
e.g. pseudo code:
if Table1.`col`=Table2.`col` then
Update Table1.`status`=1
or
if table2.`col` exists in table1.`col`
Update Table1.`status`=1
What's the best way to achieve this?
Try this one -
UPDATE table1 t1
JOIN table2 t2
ON t1.col = t2.col
SET t1.status = 1;
Table 1
col | status
-------------
jaga | 0
kala | 0
Table 2
col | status
--------------
jaga | 1
latha | 0
If Table1.col=Table2.col // So this point is fullfill jaga record.
then Update Table1.status=1 // So Table 1 jaga row status want to Update in 1.
Is I am Correct?.
Then Try
UPDATE Table1 AS t1, Table2 AS t2 SET t1.col = 1 WHERE t1.col = t2.col
Happy Codings,
update t_checkout A
INNER JOIN t_target B on A.Media_ID = B.Media_ID
set A.status = 'R'
where A.Media_ID = 45
and exists (select * from t_target where B.Media_ID = 45 and status = 'R');
The 45 is hard coded here, but the value actually comes from a php parameter.
Related
I am trying to restore the correct order in one table after certain set of modifications. I need to use UPDATE .. ORDER BY logic, but the field is in another table so INNER JOIN is required. mySQL does not like below syntax, is there any way to accomplish the same ?
SET #newOrder = 0;
UPDATE job T1 INNER JOIN job_schedule T2 ON T1.id = T2.jobID SET T1.order = (#newOrder:=#newOrder+1) WHERE T1.id = 100 ORDER BY T2.scheduledTime ASC
job table:
id actvity order
-------------------------
1 TestEngine 2
2 TestElectricity 1
job_schedule table:
id scheduledTime
-------------------------
1 2022-01-01 10:00:00
2 2022-01-01 11:00:00
After UPDATE, expected results should be:
id actvity order
-------------------------
1 TestEngine 1
2 TestElectricity 2
I have null values in table 1:
DATE_ ID Sales
2007-01 1 NULL
2007-02 2 100
2007-03 3 200
2007-04 4 NULL
and the in table 2 I have :
DATE_ ID Sales
2007-01 1 99
2007-02 2 100
2008-03 3 200
2009-04 4 300
How do I update the null values in table one with matching DATE_ and ID.
I used
update table1
set table1.sales = table2.sales
where table1.DATE_ = table2.DATE_
and table1.ID = table2.ID
but I got an error code: 1054, undefined column 'table2.ID' in 'where clause'.
I'm using MySQL.
you can use a inner joij
update table1
inner join table2 on table1.DATE_ = table2.DATE_
and table1.ID = table2.ID
and table1.sales is null
set table1.sales = table2.sales
You can try something like:
update table1 set table1.sales = (select table2.sales where table1.DATE_ = table2.DATE_ and table1.ID = table2.ID)
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
My question is how do i update a value in a table if it does not exists on another table.I checked INSERT ... ON DUPLICATE KEY UPDATE
but it describes about inserting something which updates and not insert.
My situation is like, i have two tables say (t1,t2). I want to update a column in t1 with a value if its not present in t2. Otherwise increment the value and try the update again.
So i want something like
update t1 set column = 'value' if it does not exists in t2
Can somebody suggest a solution
Here is a way to do it using the JOIN.
create table tab1 (id int , val int);
insert into tab1 values (1,1),(2,3),(3,5);
create table tab2 (id int , val int);
insert into tab2 values (4,1),(2,3),(3,5);
In the above tab1 (id = 1) not available in tab2 and using the following command we can update such values
update tab1 t1
left join tab2 t2 on t1.id = t2.id
set t1.val =
case
when t2.id IS NULL then 8
else t1.val
end
The output after the update command will look like
mysql> select * from tab1 ;
+------+------+
| id | val |
+------+------+
| 1 | 8 |
| 2 | 3 |
| 3 | 5 |
+------+------+
Also you can use EXIST which is also pretty better than doing left join
update tab1 t1 set t1.val = 10
where NOT EXISTS
(
select 1
from tab2 where tab2.id = t1.id
)
I need to replace/update values in my table according att_id for each customer_id.
The table looks like:
ID att_id customer_id value
1 5 1 name
2 30 1 12345
3 40 1
4 5 2 name2
5 30 2 12345
6 40 2
I'd like to replace it like this:
ID att_id customer_id value
1 5 1 name
2 30 1
3 40 1 12345
4 5 2 name2
5 30 2
6 40 2 12345
UPDATE: Based on your comments ...I need to find values for attribute 30, check if they are mobile phone numbers, and if it's true, write it itno value for attribute 40... your query might look like this
UPDATE table1 t1 JOIN table1 t2
ON t1.customer_id = t2.customer_id
AND t1.att_id = 40
AND t2.att_id = 30
SET t1.value = t2.value
-- ,t2.value = NULL -- uncomment if you need to clear values in att_id = 30 at the same time
WHERE t2.value REGEXP '^[+]?[0-9]+$'
You might need to tweak a regexp to match your records ("mobile phone numbers") properly
Here is SQLFiddle demo
It's hard to tell for sure from your description but if you need to swap values of att_id 30 and 40 per customer_id you may do something like this
UPDATE table1 t1 JOIN table1 t2
ON t1.customer_id = t2.customer_id
AND t1.att_id = 40
AND t2.att_id = 30
SET t1.value = t2.value,
t2.value = t1.value
Here is SQLFiddle demo
or if you need to put values of att_id = 30 to att_id = 40 and "clear" values of att_id = 30
UPDATE table1 t1 JOIN table1 t2
ON t1.customer_id = t2.customer_id
AND t1.att_id = 40
AND t2.att_id = 30
SET t1.value = t2.value,
t2.value = NULL
Here is SQLFiddle demo
Here is a general approach for swapping the values on rows with att_id equal to 30 and 40:
update t join
t t30
on t.customer_Id = t30.customer_Id and t30.att_id = 30 join
t t40
on t.customer_Id = t40.customer_Id and t40.att_id = 40 join
set t.value = (case when att_id = 30 then t40.value
when att_id = 40 then t30.value
else t.value
end)
where att_id in (30, 40);
First, you remove the value of att_id = 30
UPDATE tablename SET value="" WHERE att_id=30;
Then set the value for att_id=40
UPDATE tablename SET value="12345" WHERE att_id=40;
UPDATE tableName SET value=12345 WHERE ID=2;
UPDATE tableName SET value="" WHERE ID=3;
UPDATE tableName SET value=12345 WHERE ID=6;
UPDATE tableName SET value="" WHERE ID=5;
This are the command .See http://www.tutorialspoint.com/mysql/mysql-update-query.htm for tutorial on update.