Home > Software engineering >  How do I change a column with a query in SQL?
How do I change a column with a query in SQL?

Time:08-22

I have two tables and I want to create a new one. Here an example:

enter image description here

If Table1.A = Table2.E is TRUE, put Table2.F into Table1.B also change the column name of Table1.B in Table1.F

The result should be a new Table (Table3). Table3 is just a new modified Table of Table1.

Update:

SELECT * FROM Table1 AS t1
WHERE EXISTS
(SELECT * FROM Table2 as t2
WHERE t1.A = t2.E)

I was first trying to compare these two Tables, but I do not know how to add, change a columns or creat a new table

CodePudding user response:

What you want is a left outer join:

SELECT t1.A, t2.F, t1.C, T1.D
  FROM Table1 t1 LEFT JOIN Table2 t2 ON t1.A = t2.E;

Forgot to mention, that you can create a new table by

CREATE TABLE Table3 AS
SELECT t1.A, t2.F, t1.C, T1.D
  FROM Table1 t1 LEFT JOIN Table2 t2 ON t1.A = t2.E;
  • Related