Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres won't accept table alias before column name

I'm using a framework (Jodd) which is adding the table alias to the column names in a SQL Select. It looks like well-formed SQL, but Postgres chokes on it.

update GREETING Greeting       set Greeting.ID=5,           Greeting.NAME='World',           Greeting.PHRASE='Hello World!'   where (Greeting.ID=5) 

gives an error:

Error: ERROR: column "greeting" of relation "greeting" does not exist SQLState:  42703 

Is there a way to get Postgres to accept that SQL? My other alternative is to hack the framework, which I don't want to do.

like image 471
Ron Romero Avatar asked Jul 06 '12 21:07

Ron Romero


People also ask

How do I specify a column alias?

The basic syntax of a table alias is as follows. SELECT column1, column2.... FROM table_name AS alias_name WHERE [condition]; The basic syntax of a column alias is as follows.

How do you use column alias in ORDER BY clause?

Yes, you can certainly use column aliases in your "order by" clause. You can verify it works with the built-in mySql "user" table: select User as name,Host from user order by name; If it "errored out", then something else must have been wrong with your query.

Are column aliases allowed in FROM clause?

Column Alias Column aliases can be used for derived columns. Column aliases can be used with GROUP BY and ORDER BY clauses. We cannot use a column alias with WHERE and HAVING clauses.

Can we use alias in update statement PostgreSQL?

Check documentation on UPDATE statement, specifically for the column part: it is illegal to prefix columns with table alias in the SET clause.


1 Answers

The problem is that you include the table alias in SET clause, in the columns. See the documentation of UPDATE in Postgres docs:

column

The name of a column in table. The column name can be qualified with a subfield name or array subscript, if needed. Do not include the table's name in the specification of a target column — for example, UPDATE tab SET tab.col = 1 is invalid.

This is valid in Postgres:

update GREETING Greeting  set      NAME='World',      PHRASE='Hello World!'  where Greeting.ID=5 ; 
like image 102
ypercubeᵀᴹ Avatar answered Sep 17 '22 17:09

ypercubeᵀᴹ