Re: query with table alias

From: Michael Glaesemann <grzm(at)myrealbox(dot)com>
To: "Rodríguez Rodríguez, Pere" <prr(at)hosppal(dot)es>
Cc: "'pgsql-general(at)postgresql(dot)org'" <pgsql-general(at)postgresql(dot)org>
Subject: Re: query with table alias
Date: 2004-11-12 11:09:04
Message-ID: 44047472-349B-11D9-9818-000A95C88220@myrealbox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Nov 12, 2004, at 7:48 PM, Rodríguez Rodríguez, Pere wrote:

> prr=# select foo.c1, f.c2 from foo f;  -- >>> Incorrect result <<<
>  c1 | c2
> ----+----
>   1 |  1
>   2 |  1
>   1 |  2
>   2 |  2
> (4 filas)

If you alias a table, you can only reference the table using the alias.
It is in effect renamed: foo is no longer foo. It is only f after you
alias foo to f.

> The result of the "select foo.c1, f.c2 from foo f" isn't correct, it
> do a cartesian product of foo table.

When you do reference foo, PostgreSQL adds foo to the FROM list if you
have the ADD_MISSING_FROM configuration parameter set to TRUE in
postgresql.conf.

Thus, PostgreSQL considers your query to be
SELECT foo.c1, f.c2 FROM foo f, foo;

which results in the Cartesian join your are seeing.

Some people like this, some people don't. (I'm one of the latter.) If
you would rather PostgreSQL throws an error in this situation, set
ADD_MISSING_FROM to FALSE.

Hope this helps.

Michael Glaesemann
grzm myrealbox com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Patrick Fiche 2004-11-12 11:13:24 Re: query with table alias
Previous Message Rodríguez Rodríguez, Pere 2004-11-12 10:48:44 query with table alias