MYSQL - UPDATE multiple rows from another table - mysql

I have 2 tables. one from yesterday (300k rows) and another one from today with the same count of rows but the data changes in some columns.
Those two tables have around 120 columns.
How can i update only the changes.
I have tried using delete :
delete from tableA
where id in (select id from tableB)
But it too slow.
Also tried
update tableA inner join tableB
on tableA.id=TableB.id
And it didn't worked.

You have to set the values in your update query to get the changes.
Example:
update tableA inner join tableB on tableA.id=TableB.id
set tableA.col1=TableB.col1,
tableA.col2=TableB.col2,
tableA.col3=TableB.col3;
and also you can add more conditions in where clause to make query run on filtered records.

delete from tableA
where id in (select id from tableB)
Instead of above query try this:-
Delete tableA from tableA left Join tableB ON tableA.id = tableB.id where tableB.id IS NOT NULL;

Related

UPDATE A COLUMN WITH DIFFERENT VALUES IN ROWS

I want to update ColumnX of TableA with the values of ColumnY in TableB.
This two tables have in common the atrribute id.
Is it possible, when I try an UPDATE code I get
Subquery returns multiple rows
The subquery looks something like this:
UPDATE TableA
SET ColumnX = (SELECT ColumnY FROM TableB WHERE tableA.id=tableA.id);
Try:
UPDATE TableA
SET ColumnX = (SELECT ColumnY FROM TableB WHERE tableA.id=tableB.id);
And make sure that SELECT ColumnY FROM TableB WHERE tableA.id in (SELECT id FROM TableB ) returns 1 value
your where clause is currently using tableA.id=tableA.id, which will be true for every row. Try:
UPDATE TableA
SET ColumnX = (SELECT ColumnY FROM TableB WHERE tableB.id=tableA.id);
You can use joins like when you are selecting rows, for example:
UPDATE TableA, TableB set TableA.ColumnX=TableB.ColumnY WHERE TableA.id=TableB.id
Just change the name in your where clause condition after = as you have same table name resulting into multiple results to TableB.id or try below
UPDATE TableA A
Join TableB B
On
A.id= B.id
SET A.ColumnX = B.ColumnY

MySql Join Issue When Join Two Tables

I have some doubt in MySQL joins
I have 2 tables for example TableA,TableB
TableA primary key is Foreign key of Table B
So I'm using inner join to get matched values but TableB have one column for row activate status so all active status is zero means I need to get that record or else I need to skip that record.
My query:.
Select * From TableA a inner join TableB b on a.id=b.aid where b.isActive=0;
The above query was return value if any one value is true
For example any one of is active row true. But I need to check all row is zero if it's all value zero means I need to return that so how I do that..?
Thanks in advance.
Select a.*
From TableA a
where not exists(SELECT 1 FROM TableB b WHERE a.id=b.aid AND b.isActive=1);
You can use your query change the filter as follows:
SELECT * FROM TableA a
JOIN TableB b ON a.id=b.aid
WHERE a.id NOT IN(SELECT aid FROM TableB WHERE isActive=1)

MySQL - Selecting rows where fields not equal

I have a little problem with an SQL query: I have 'TableA' with a field 'TableA.b' that contains an ID for 'TableB'. I want to select all rows from 'TableB' that don't have an ID that equals any field 'TableA.b'. With other words, I need every row from TableB that's not referred to by any row from TableA in field .
I tried a Query like this :
SELECT DISTINCT TableB.* FROM TableA, TableB Where TableA.b != TableB.ID
But the result contains a row that is also returned by the negation, i.e. where both fields have the same value.
Any ideas?
What you need is LEFT (or RIGHT) JOIN.
SELECT TableB.* FROM TableA
LEFT JOIN TableB on TableA.b = TableB.ID
WHERE TableA.b IS NULL
While it's possible to do the same with a subquery as in some of the otehr answers. A join will often be faster.
A LEFT [OUTER] JOIN can be faster than an equivalent subquery because
the server might be able to optimize it better—a fact that is not
specific to MySQL Server alone. Prior to SQL-92, outer joins did not
exist, so subqueries were the only way to do certain things. Today,
MySQL Server and many other modern database systems offer a wide range
of outer join types.
First, select all ids from TableA:
SELECT DISTINCT b FROM TableA
Then use that result to select all rows in TableB that have an id that does not exist in this set by using the above query as a subquery:
SELECT * FROM TableB WHERE ID NOT IN (SELECT DISTINCT b FROM TableA)
Hope this helps.
You can try this
SELECT TableB.* FROM TableB
WHERE ID NOT IN
(SELECT b from TableA);
Use NOT IN in SELECT Query.
SELECT * FROM TableB t1 WHERE t1.ID NOT IN (SELECT t2.b FROM TableA t2);
You can use right join also.
Try this:
SELECT DISTINCT TableB.* FROM tablea RIGHT JOIN TableB ON TableA.b = Tableb.ID WHERE TableA.B IS NULL

Update a table after inserting records with an Inner Join

I insert into TableA using a Select/Inner Join from TableB and TableC.
Insert into TableA (C,S,M,C100)
SELECT C,S,M,group_concat(CID) FROM TableB
INNER JOIN TableC
ON TableB.CID= TableC.CID and P>=100 group by C,S,M
Now I need to update those records in two ways. One is identical to the first but now I want to update a different field with P<100, in essence:
Insert into TableA (C,S,M,C0)
SELECT C,S,M,group_concat(CID) FROM TableB
INNER JOIN TableC
ON TableB.CID= TableC.CID and P<100 group by C,S,M
Except I don't want new records I want to update where TableA C,S,M match
The second thing I want to do is similar, but involves updating from a different table but in almost an identical manner
Insert into TableA (C,S,M,C100)
SELECT C,S,M,group_concat(CID) FROM TableD
INNER JOIN TableE
ON TableD.CID= TableD.CID and P>=100 group by C,S,M
In other words I could create each pass as separate inserts but would end up with duplicate records of C,S,M.
Is there a way to do the passes after the first insert as Updates OR is there a way to do them each as Inserts and afterwards combine the records where C,S,M are identical?
Use an update with join :
UPDATE TableA
join (select C,S,M,group_concat(CID) as newCol
FROM TableB
where P<100
group by C,S,M) t
ON (tableA.c = t.c and tableA.s = t.s and TableA.M = t.m)
SET <YourColumn> = t.newCol

Update TableA with values from TableB?

If I have 2 tables, each have a product_stat DECIMAL and product_id INT column
I want to run a query that will append the product_stat from TableA to TableB on product_id. Then truncate TableA
Basically I am collecting data and temporarily storing it in TableA, and once a day I want to move the data to TableB. So that TableB only has the data shifted once a day.
The quich solution is to use a subquery
UPDATE tableB SET product_stat = (
SELECT product_stat FROM tableA
WHERE tableB.product_id = tableA.product_id
)
But you can use UPDATE in conjunction with JOIN, which will have a better performance
UPDATE tableB
INNER JOIN tableA ON tableB.product_id = tableA.product_id
SET tableB.product_stat = tableA.product_stat
UPDATE Authors AS A, Books AS B SET AuthorLastName = 'Wats' WHERE B.AuthID = A.AuthID AND AND ArticleTitle='Something';