Answer the question
In order to leave comments, you need to log in
How to add data to a column from another table in SQL?
There are two tables - TableA and TableB. Each of them has a column - Product code.
I have created a new empty column Title in table TableA . How to fill it with data from table B using the Product Code as the basis for matching? I tried creating a combined query. in two versions, and both options overwrite the data of all columns, except for the one that is filled in TableA, regardless of EXCEPT or an explicit indication of the column.
What am I doing wrong? Are there any other options? Thank you
1.
INSERT INTO TableA (* EXCEPT(product code, quantity)) SELECT
Name FROM TableA
LEFT OUTER JOIN TableB
ON TableA.Product code=TableB.Product code
2.
INSERT INTO TableA (Name) SELECT
Name FROM TableA
LEFT OUTER JOIN TableB
ON TableA.Product code=TableB.Product code
Answer the question
In order to leave comments, you need to log in
CREATE TABLE product_a
(
id SMALLINT AUTO_INCREMENT,
name_a VARCHAR(10),
code_a INTEGER,
PRIMARY KEY (id)
);
CREATE TABLE product_b
(
id SMALLINT AUTO_INCREMENT,
name_b VARCHAR(10),
code_b INTEGER,
PRIMARY KEY (id)
);
INSERT INTO product_b VALUES (null, 'prod_b_3', 300);
ALTER TABLE product_a ADD name_b VARCHAR(10);
UPDATE product_a AS pa
SET pa.name_b = (SELECT pb.name_b FROM product_b AS pb WHERE pb.code_b = pa.code_a);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question