complex sql query to update a table if an id is in - mysql

i would like to update massively a table regarding an other one.
I have an commun id and i would like to update a row if the id is in the second table.
Table 1 :
ID_table1|is_in_db2
Table 2 :
ID_table2|ID_table1
I want to update is_in_db2 to 1 if the ID_table1 is in table 2

You want to join the tables using an inner join on the ID.
This will update the specified column for any row that appears in both tables.
UPDATE
table1
INNER JOIN table2 ON table1.ID = table2.ID_table1
SET
table1.is_in_db2 = 1;

Related

How to differentiate name of columns in different tables?

I have two tables, their structure looks like this:
table 1: id, name, description
table 2: id, otherDescription
I want to set the value of "description" in table #1 to value "otherDescription" from table #2 respectively this id. I wrote the query:
UPDATE `table_1` SET description = (SELECT oldDescription FROM table_2 WHERE id = id))
How MySql will knows that in expression WHERE id = id first id taken from one table, and other id taken from second table? How to write this query correct? I think here must be used AS, but i dont know where
you can use table mane eg :
UPDATE table_1
INNER JOIN table_2 ON table_1.id = table_2.id
SET table_1.description = table_2.oldDescription
or table name alias
UPDATE table_1 a
INNER JOIN table_2 b ON a.id = b.id
SET a.description = b.oldDescription
You can join the two tables in the UPDATE, and give each an alias. The you identify the columns using alias.columnname:
UPDATE `table1` a
JOIN `table1` b ON a.`id` = b.`id`
SET a.description` = b.`otherDescription`

MySql - Finding duplicates and deleting them in two tables

I have a very big amount of data on my tables (table 1 and table 2) so I am asking this simple question because I want to have the most efficient way to do this:
How can I delete all the entries from table 1, which are also in table 2 (identified by attribute 'atribxy'). So there will be only exclusive entries in table 1 and in table 2. How can I achieve this in the most efficent way using a SQL-query?
There are many ways to do so:
Using JOIN
DELETE table2
FROM table2
INNER JOIN table1 ON table1.atribxy = table2.atribxy
Using IN
DELETE FROM table2 WHERE atribxy IN (SELECT atribxy FROM table1)
Using EXIST
DELETE FROM table2 t2 WHERE EXISTS ( SELECT 1 FROM table1 t1 WHERE t1.atribxy = t2.atribxy)

update query in sql based on condition fro two tables

I need to build a update query. My existing code looks like this.
update table1
set data_plan=(select d.data_plan from table1 m,table2 d
where m.msidn = d.msidn and m.data_plan!=d.data_plan);
table 1 has columns msisdn and data_plan, table 2 also has same columns. I want to update the table1 data_plan column depending on some condition which I get through select query. But when I run the code I get this error.
You can't specify target table 'msisdn1' for update in FROM clause
Try it this way
update table1 m join table2 d
on m.msidn = d.msidn
and m.data_plan != d.data_plan
set m.data_plan = d.data_plan
Try this ...
UPDATE table1 SET m.data_plan=d.data_plan
FROM table1 m
INNER JOIN table2 d ON m.msidn = d.msidn and m.data_plan!=d.data_plan

Is it possible that update a table with results with another nested query which inlude the queried table itself?

I have two tables. First has id (AI) field and old_id (Int) field.
Second table has a referred field p_id from first tables old_id as a relation point.
I want to update p_id to firstTable.id values. Is it possible to update secondTable.p_id with the value returned from firstTable.id ?
Here is my test sql:
UPDATE secondTable sT
SET sT.p_id = (
SELECT fT.id
FROM firstTable fT
WHERE fT.old_id = secondTable.p_id
)
You can update with JOIN like this:
UPDATE secondTable sT
INNER JOIN firstTable fT ON fT.old_id=secondTable.p_id
SET sT.p_id = fT.id;

How to update based on anoter row in the same table?

I want to create a query that updates an int based on the int of the row with an id that is 1 higher.
I have tried this query, but it says that i can't label the table in an update statement. But how do i reference it in my subquery?
update t1 a set `int1` = (select `int1` from t1 b where b.id=a.id+1);
How can I overcome that I can't use an alias?
Try this one -
UPDATE
t1 a
JOIN t1 b
ON b.id = a.id + 1
SET
a.int1 = b.int1;
If there are holes in id values, the query may be changed.