2

I have a few tables with this sort of format

myTable: id | Table1_id | Table2_id | Table3_id | value

Table1: id | text | ...

Table2: id | text | ...

Table3: id | text | ...

I want to create a view that replaces the ID's with the associated text. I can get that using 3 join statements like shown:

SELECT table1.id, table2.id, table3.id, value
FROM myTable

INNER JOIN Table1
  ON Table1_id = Table1.id
INNER JOIN Table2
  ON Table2_id = Table2.id
INNER JOIN Table3
  ON Table3_id = Table3.id;

When I try to create a view using

CREATE VIEW myView AS (

SELECT table1.id, table2.id, table3.id, value
FROM myTable

   INNER JOIN Table1
     ON Table1_id = Table1.id  -- I tried as t1 but that doesn't work
   INNER JOIN Table2
     ON Table2_id = Table2.id
   INNER JOIN Table3
     ON Table3_id = Table3.id
);

I get an ORA-00957: duplicate column name error, which makes sense, they all have the same id column. How do I use an alias or something similar so I can create the view which has a format like this:

myTable: id | t1 | t2 | t3 | value

where tn is the column name that contains tn.text.

2
  • 1
    CREATE VIEW myView AS ( SELECT table1.id AS t1id, table2.id AS t2id, table3.id AS t3id, value ...
    – Akina
    Commented Jun 5, 2018 at 20:09
  • didn't see you post that as I was typing an answer, thanks! Commented Jun 5, 2018 at 20:12

1 Answer 1

1

I figured it out, the alias goes up with the SELECT statement so the query would be:

CREATE VIEW myView as (

  SELECT table1.id as t1, table2.id as t2, table3.id as t3, value
  FROM myTable

  INNER JOIN Table1
    ON Table1_id = Table1.id
  INNER JOIN Table2
   ON Table2_id = Table2.id
  INNER JOIN Table3
   ON Table3_id = Table3.id

);

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.