sqlpostgresqlquoted-identifier

Postgres query with uppercase letters in Column name


I searched and followed various online guide however am not successful with the following query, which while doesnt give errors, it does not get the expected results.

table gBInfo:
(columns) id,userId, fromGB, fromGBId      

table User:
(columns) id, firstName, lastName

query:

select "id", "firstName", "fromGB" 
from "User" join "gBInfo"
on 'User.id' = 'gBInfo.userId';

What do I get this query to work, with uppercase letters in column and table names.


Solution

  • Single quotes are for string constants, not for identifiers. And each part of a multi-part identifier needs to be quoted separately:

    select "id", "firstName", "fromGB" 
    from "User" 
      join "gBInfo" on "User".id = "gBInfo"."userId";
    

    The condition 'User.id' = 'gBInfo.userId' simply compares the string value User.id with the string value gBInfo.userId which are obviously never equal.