Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres column does not exist but it's there with alias [duplicate]

Tags:

sql

postgresql

I have a generated Postgres query

select ..., s.name as supplier, ...
from ... supplier s ...
where ...
      AND (cast(supplier as varchar(255)) ilike '%test%')
      ...
ORDER BY supplier desc nulls last

I get this error:

org.postgresql.util.PSQLException: ERROR: column "supplier" does not exist

if I replace the "supplier" with s.name then it's ok but I can't do that. I have this issue on other columns too but there are columns which are working fine.

There are some restriction that I can't use alias the same as the table name or what can be the problem?

like image 212
Zsolt Balogh Avatar asked Jun 17 '15 15:06

Zsolt Balogh


People also ask

Can we use alias in PostgreSQL?

In PostgreSQL, an alias is a temporary alternative name for columns, tables, views, materialized views, etc. in a query. Aliases are assigned during query execution and aren't stored in the database or on disk. By using column aliases, the query output can become more meaningful.

Can we use alias name in WHERE condition?

The WHERE clause can contain non-correlated aliases and correlated aliases.

How to use alias in PostgreSQL?

Column AliasSELECT column_name AS alias_name FROM table; or, SELECT column_name alias_name FROM table; Below Syntax is for column alias used with expressions: SELECT expression alias_name FROM table; The primary use of column alias is to make the output of a query more meaningful.

Can we use alias column name in WHERE clause?

In PROC SQL, a column alias can be used in a WHERE clause, ON clause, GROUP BY clause, HAVING clause, or ORDER BY clause. In the ANSI SQL standard and ISO SQL standard, the value that is associated with a column alias does not need to be available until the ORDER BY clause is executed.


1 Answers

I found the explanation here: http://www.postgresql.org/message-id/[email protected]

I can't use the alias from the select in the WHERE clause.

like image 70
Zsolt Balogh Avatar answered Oct 11 '22 05:10

Zsolt Balogh