UPDATE A COLUMN WITH DIFFERENT VALUES IN ROWS - mysql

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

Related

How to update a field of a table that is inner join of another table in MySQL?

I have two tables: tablea and tableb. Both of them have three columns called columna, columnb ,columnc.
Now I want to do this:
If tablea.columnb equals tableb.columnb,then set tablea.columnc = tableb.columnc. I have written the sql and it works well, but I think that there must be a better way to do this? Can anyone help me to optimize my sql statement, or is there any other way?
UPDATE tablea ta
SET
ta.columnc = (
SELECT columnc FROM tableb
WHERE ta.columnb = tableb.columnb
)
WHERE ta.columnb IN (
SELECT columnb FROM tableb
WHERE ta.columnb = tableb.columnb
)
You can try this simple query
Update tablea ta,table tb set ta.columnc=tb.columnc where ta.columnb =tb.columnb;

MYSQL - UPDATE multiple rows from another table

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;

Get row count of tableA depending on existence of a FK in tableB

I want to get row count in tableA if and only if rowA_x does not have a FK pointing to rowB_x in tableB.
tableA:
id | id_tableB
tableB
id | ...
So basically the rows in tableA should only be counted if the column id_tableA does not exist as id in tableB.
Is there a clean way to do such counting. I have around ~500.000 rows.
There are several (not 100% sure about the MySQL syntax, so this might require some tweaking):
Subselect with NOT IN:
select count(*) from tableA where id_tableB not in (select id from tableB);
Subselect with NOT EXISTS:
select * from tableA a
where NOT EXISTS (select null from tableB b where a.id_tableB = b.id);
OUTER JOIN:
select count(*) from (
select a.*, b.id as b_id
from tableA a
left join tableB b on a.id_tableB = b.id)
where b_id IS NULL;
Which of these is the fastest depends on your data, but usually, a JOIN is more efficient than a subquery.
Maybe you could use WHERE NOT EXISTS() structure.
If I understood well your question, your final query will look like:
SELECT COUNT(*)
FROM tableA
WHERE NOT EXISTS (SELECT id FROM tableB WHERE tableA.id = tableB.id_tableA)
The most efficient way I can think of doing this is using a sub-query:
SELECT COUNT(*)
FROM tableA
WHERE id_tableB NOT IN (
SELECT id
FROM tableB
)
;
Edit: However, upon further consideration, the below query may actually be more efficient as it uses a LEFT OUTER JOIN rather than a sub-query.
SELECT COUNT(*)
FROM tableA A
LEFT OUTER JOIN tableB B
ON A.id_tableB= B.id
WHERE B.id IS NULL
;

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';

Inserting distinct entries into the database

I have two tables with exactly the same fields. Table A contains 7160 records and table B 7130 records.Now I want to insert distinct records from table A into table B such that B should not have any duplicate entry in it. How should I go about doing this?
This basically selects records that are in A that are not in B. It would work, but you might have to tweak the field you use to uniquely identify a record. In this example I used field 'ID' but you might have to change that to A.field1 = B.field1 AND A.field2 = B.field2 etc.
INSERT INTO TABLEB
(
SELECT A.*
FROM TABLEA A
LEFT JOIN TABLEB B ON A.ID = B.ID
WHERE B.ID IS NULL
)
You can use a "union" query to combine the results from multiple tables into a single result set. "union" will only return distinct rows from all tables.
See this page for more info:
http://www.tutorialspoint.com/mysql/mysql-union-keyword.htm
insert into tableB (id)
select t1.id from tableA t1
where t1.id not in (select t2.id from tableB t2)