0

To be a bit clear I have 3 different tables with several columns each I want to kind of create a final table to help me explain myself better

Table A:

id a2 a3 a4 a5 a6
1 11 12 13 14 15

Table B:

id b2 b3 b4 b5 b6
1 21 22 23 24 25

Table C:

id c2 c3 c4 c5 c6
1 31 32 33 34 35

What I want to achieve is creating a new table with values from the other 3 tables they all have the same id:

FinalTABLE:

id a2 a3 b2 b5 c3 c4
1 11 12 22 24 32 33
1
  • Does id is primary key in each table? Does any id value which is present in one table is also present in another tables with guarantee?
    – Akina
    Feb 2, 2023 at 10:17

1 Answer 1

0

Assuming you really mean to "join on id":

SELECT a.id, a.a2, a.a3,
             b.b2, b.b5,
             ...
    FROM a
    JOIN b  USING(id)
    JOIN c  USING(id)

You mention building a new table from those three tables -- will you keep all the tables? This is a RDBMS no-no, namely "redundant data".

To keep them all:

CREATE TABLE ABC
    SELECT ... -- that select above;

To use a "VIEW" (no redundancy, but fetch when needed):

CREATE VIEW ABC ...
    AS
    SELECT ... -- that select above;

To put into a different "DATABASE", use the "db.tbl" syntax:

CREATE ... db2.abc
    SELECT ... FROM db1.a  JOIN db1.b ...;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.