Iam so weak in SQL, please help. I have 2 tables with same columns: quantity and SKU. Need UPDATE Table_2 quantity with values from Table_1 quantity for strings with same SKU.
Seems it must looks like this:
UPDATE Table_2
SET Table_2.quantity = Table_1.quantity
WHERE Table_2.SKU = Table_1.SKU
;
How make that request properly?
You could use a inner join update
UPDATE Table_2
INNER JOIN able_1 ON Table_2.SKU = Table_1.SKU
SET Table_2.quantity = Table_1.quantity
You need to use aliases and join like:
Update t2
set t2.quantity = t1.quantity
from Table_2 as t2
inner join Table_1 as t1 on t1.SKU = t2.SKU
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 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
I want to create a generic query which work with MySQL and PostgreSQL.
For this query, I need to select all column in 3 tables but the result need to have a distinct clause on the ID to eliminate duplicates rows.
Actually in the database, there is 6 records but just 3 are differents:
One of them appears 3 times, an other appears 2 and the last just on.
Records which appears many time are exactly the same and I just want keep one of each.
This is a picture of the 6 records:
And me I want that:
Here is the MySQL Query:
SELECT
*
FROM
table_1
INNER JOIN table_2
ON table_1.id = table_2.table_1_id
INNER JOIN table_3
ON table_2.table_3_id = table_3.id
WHERE
table_3.type = 'foo'
GROUP BY
table_1.id
And this is the PostgreSQL query:
SELECT DISTINCT ON (table_1.id)
*
FROM
table_1
INNER JOIN table_2
ON table_1.id = table_2.table_1.id
INNER JOIN table_3
ON table_2.table_3_id = table_3.id
WHERE
table_3.type = 'foo'
I don't find how to create just one query which work with MySQL and PostgreSQL
Try to first get the distinct ids of the first table, then join the rest on to them.
Though, you will still get somewhat random values for table_1 I guess, if there are multiples ids with different values to them and you not specifying which you want.
SELECT
table_1.*, table_2.*, table_3.*
FROM
(SELECT table_1.id FROM table_1 GROUP BY table_1.id) AS distinctIds
INNER JOIN table_1 ON table_1.id = distinctIds.id
INNER JOIN table_2 ON distinctIds.id = table_2.table_1_id
INNER JOIN table_3 ON table_2.table_3_id = table_3.id
WHERE
table_3.type = "foo"
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 got two tables. I want to update MODEL in table2 when ITEM in table1 equals ITEM in table2.
Any Ideas?
In MySQL, you do it like this
UPDATE table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id
SET t1.col1 = t2.col1,
t1.col2 = t2.col2
If I understand correctly, you just want to perform an UPDATE on table2 based on, presumably, foreign keys?
If that's right, this should work:
UPDATE
table2
JOIN table1
ON table1.ITEM = table2.ITEM
SET
MODEL = 'new value';
The table declaration in an UPDATE statement is the same as is specified in a SELECT statement - so you can use any type of JOIN that fits your table/data.
Docs for UPDATE, SELECT.
If you could add an actual query attempt, or something, that might be helpful. Can you try something like the following:
UPDATE table2 JOIN table1 ON table2.ITEM = table1.ITEM SET MODEL = ?