Search code examples
sqldatabasepostgresqlselectview

Postgres "ERROR: syntax error at or near" but no apparent syntax error


I'm running into kind of an odd problem. I'm trying to make a view in PostgreSQL, but I'm getting the error mentioned in my title. From what I can tell I don't have any syntax errors, but Postgres is still throwing an error. What am I doing wrong?

Here's the error I'm running into:

ERROR:  syntax error at or near "FROM"
LINE 3: period, start, "end" FROM Periods,

Here's the view and the corresponding table I'm running into the issue with:

CREATE VIEW ReservationsView AS
SELECT code, date FROM Reservations,
period, start, "end" FROM Periods,
room FROM Rooms,
name FROM Users
FROM Reservations A
INNER JOIN Periods B
ON A.room = B.room
INNER JOIN Rooms C
ON A.room = C.room
INNER JOIN Users D
ON A."user" = D."user"
ORDER BY A.code DESC;
CREATE TABLE Reservations (
    code SERIAL PRIMARY KEY, 
    abbr VARCHAR(5), 
    room INT, 
    date DATE NOT NULL, 
    period CHAR(1), 
    "user" INT, 
    FOREIGN KEY (abbr, room) REFERENCES Rooms (abbr, room), 
    FOREIGN KEY (period) REFERENCES Periods (period),
    FOREIGN KEY ("user") REFERENCES Users ("user")
);

CREATE TABLE Periods (
    period CHAR(1) PRIMARY KEY, 
    start TIME NOT NULL, 
    "end" TIME NOT NULL
);

Solution

  • "No apparent syntax error" :)

    CREATE VIEW reservations_view AS
    SELECT a.code, a.date
         , b.period, b.start, b."end"
         , c.room
         , d.name
    FROM   reservations a
    JOIN   periods b USING (room)
    JOIN   rooms   c USING (room)
    JOIN   users   d ON d."user" = a."user"
    ORDER  BY a.code DESC;
    

    Start reading the manual here.

    And don't use reserved words like "end" or "user" as identifier, that's asking for trouble. Use legal, lower-case, unquoted names to make your life with Postgres easier. See:

    And don't use the obsolete data type char(N). See: