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';
Related
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
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
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;
Two tables. 8 fields in each. Both tables have the same data, one with
137,002 record (tablea) and one with 135,759 records (tableb). Both tables share a common primary field if three columns (qid, sid, aid).
Is there a single query that will.
1) compare tablea to tableb on the primary field
and
2) if the record is in tablea and not tableb copy the record from tablea to tableb
I would rather be able to update tableb with an sql query rather than writing a php loop to go through the 137,002 and do a compare on each one.
Thanks
That should be smth looking like:
insert into table2 (qid, sid ...)
select
t1.qid,
t1.sid,
...
from table1 t1
where
not exist (select t2.qid, t2.sid, ... from table2 t2 where t2.qid = t1.qid and t2.sid = t1.sid...)
INSERT INTO tableb AS b
(SELECT * FROM tablea AS a WHERE NOT EXISTS (SELECT * FROM tableb AS b2 WHERE b2.id = a.id))
Use merge...and use insert only....not update.
So, the following worked.
insert into f_step_ans (`qid`, `assid`, `sid`, `sas`, `cas`, `etim`, `stim`, `endtim`, `fil`)
select
t1.qid,
t1.assid,
t1.sid,
t1.sas,
t1.cas,
t1.etim,
t1.stim,
t1.endtim,
t1.fil
from f_step_ans_back t1
where
not exists (select t2.qid, t2.sid,t2.assid from f_step_ans as t2 where t2.qid = t1.qid and t2.assid = t1.assid and t2.sid = t1.sid)
1,588 records were moved from the f_step_ans_back table (old backup table) to the f_step_ans table (partially recovered backup + new data). Reporting shows that everything is working like it should be. Thank you all for the help.
I'm working with mysql:
btableA has tableB_id column
tableB has some_interestring_column_on_TableB
I want (pseudo sql below):
delete from tableA
where the associated row in tableB (via tableB_id) has
some_interestring_column_on_TableB = 'interestingValue'
Please help me to translate the pseudo sql into real sql.
Try this:
DELETE TableA
WHERE tableB_id IN (
SELECT id FROM TableB
WHERE interestring_column='pizza');
MySQL supports JOINs in the DELETE statement, as well as deleting from multiple tables in a single statement. The following will only delete from TABLEA:
DELETE ta
FROM TABLEA ta
JOIN TABLEB tb ON b.id = a.tableb_id
AND b.col = 'some value'
If you wanted to delete from both tables, use:
DELETE ta, tb
FROM TABLEA ta
JOIN TABLEB tb ON b.id = a.tableb_id
AND b.col = 'some value'
That said, this support is very uncommon in other databases -- you'd have to use IN or EXISTS in most cases.
I don't know mysql syntax but I'm thinking something like (from mssql):
delete from tableA
where tableA.tableB_id in
(select tableB.id from tableB
where tableB.some_interesting_column_on_TableB = 'interestingValue')