I created table_1 with a list of products and columns for price and quantity (both are currently NULL as a default for an empty value)
I have another table (table_2) with the same columns with info about price and quantity for each product.
How do I update table_1 with info from table_2?
I've tried to use subqueries with both UPDATE and INSERT INTO commands - Both didn't work.
I pasted the two tables here:
https://justpaste.it/8qusd
Consider the update/join syntax:
update table1 t1
inner join table2 t2 on t2.id = t1.id
set t1.price = t2.price, t1.quantity = t2.quantity
Related
Hi I have two table one like this one:
table1
and one like this:
table2
I would like to update all the fields on the table2 column "newID" based on this rules: if (table2.ID = table1.ID_actual or table2.ID=table1.ID_old) then table2.newID = table1.newID
How can I resolve this problem ?
You need a join of the 2 tables in the UPDATE statement:
UPDATE table2 t2
INNER JOIN table1 t1 ON t2.ID IN (t1.ID_actual, t1.ID_old)
SET t2.newID = t1.newID
I have an empty table (t1) and I want to insert or update the t1.uid column from another table's (t2) GROUP BY uid values.
So far I have tried like this:
UPDATE table1 t1 JOIN
(SELECT uid FROM table2 GROUP BY uid) t2
SET t1.uid = t2.uid;
but it's not working for me.
N.B. I've got a massive data set for which group by (uid from table-t2) results giving me total 1114732 results which I have to insert/update in t1 table's uid column.
Please try this:
Insert into table1(uid)
select distinct uid from table2
If table1 is empty, then UPDATE is not the correct verb. Would this suit your needs?
INSERT into table1 SELECT distinct uid from table2;
INSERT ... SELECT docs
Need help with a mysql query. I have 2 tables - table1 and table2. I am trying to update a field in table1 which is not included in table2 - named Status. And I want to update that field with the value of 'A'. table1 and table2 DO have a field in common - named Member_ID. Here is my query that is giving me errors:
UPDATE table1 SET Status='A' WHERE Member_ID=table2.Member_ID;
Is there some type of join that is needed? Any help would be appreciated. Thanks.
cdr6800
This can be done with exists
UPDATE table1 s
SET s.status = 'A'
WHERE EXISTS(select 1 from table2 t
WHERE t.member_id = s.member_id)
You have to JOIN table1 to table2 like this:
UPDATE table1 AS t1
INNER JOIN table2 AS t2 ON t1.Member_ID = t2.Member_ID
SET t1.Status='A'
This will update all table1 records having a Member_ID value that also exists in table2.
I have two tables t1, t2 that I have created and loaded data from a CSV into these.
I had to then create a new PK column as the existing columns (t1.old_id, t2.old_id) are strings that would naturally be a PK are not absolutely fixed (this seems to be advised against?)
so I created a id PK INT AUTO_INCREMENT in each table
as one record in t1 is linked to many in t2 and I want to maintain referential integrity between these two tables.
I believe what i need to do is create an id INT NOT NULL in t2 as an FK
This t2.id is blank at the moment (as it is dependent ont1.id`)
Am I right in thinking I need an UPDATE query with a JOIN of some description to make this work?
The following produces the data exactly that I want to update into my t2.id column - but I don't know how to do the update
select t1.id
from t1
inner join t2
on t1.old_id = t2.old_id
You can use a join in your UPDATE statement like this:
UPDATE t2
JOIN t1 ON t1.old_id = t2.old_id
SET t2.id = t1.id
You can use a correlated UPDATE query like this
UPDATE t2
SET id = (SELECT MAX(t1.id) FROM t1 WHERE t1.old_id = t2.old_id);
*Assuming you have a single t1.id for each t1.old_id
On a Separate Note, You should name t2.id like t2.t1ID so as to remove ambiguity if and when you have a identity column in t2 as well named id
How can i construct a SQL query to delete how i want.
I have two tables.
Table 1.
ID: Some Random Not Significant To This Question Columns : DateTime : UserID
Table 2.
ID: Some Random Not Significant To This Question Columns : DateTime : UserID
The two tables are related by DateTime and UserID
Is there anyway i can create a query so that it deletes from table 2 if no rows in table1 have a matching DateTime & UserID.
Thanks
You can use LEFT JOIN :
DELETE table2
FROM table2 t2 LEFT JOIN table1 t1 ON t1.`DateTime` = t2.`DateTime`
AND t1.`UserID` = t2.`UserID`
WHERE t1.`UserID` IS NULL
DELETE
FROM table2 t2
WHERE NOT EXISTS
(
SELECT NULL
FROM table1 t1
WHERE (t1.userId, t1.dateTime) = (t2.userId, t2.dateTime)
)
First of all: create a backup before you delete lots of records :)
The idea:
DELETE FROM
table1
WHERE
NOT EXISTS (SELECT 1 FROM table2 WHERE table1.referenceColumn = table2.referenceColumn)
You can check which records will be deleted by replacing the DELETE with SELECT *
And now the solution
DELETE FROM
table2
WHERE
NOT EXISTS (
SELECT 1 FROM
table1
WHERE
table2.UserID = table1.UserID
AND table2.DateTime = table1.DateTime
)