Re: ambiguous

From: "Brett W(dot) McCoy" <bmccoy(at)chapelperilous(dot)net>
To: si <s(at)remail(dot)net>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: ambiguous
Date: 2001-03-07 16:09:50
Message-ID: Pine.LNX.4.30.0103071058020.14637-100000@chapelperilous.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, 7 Mar 2001, si wrote:

> If I assign the same name to 2 columns in 2 different tables:
>
> +--------------------------------------+
> | DEPARTMENT |
> +----------+----------------+----------+
> | LOCATION | DESCRIPTION | DEPT_NO |
> +----------+----------------+----------+
> | Bedrock | Administration | 1 |
> +----------+----------------+----------+
>
> +--------------------------------+
> | EMPLOYEE |
> +---------+------------+---------+
> | EMPL_ID | NAME_LAST | DEPT_NO |
> +---------+------------+---------+
> | 1 | Slate | 1 |
> +---------+------------+---------+
>
> select * from DEPARTMENT, Employee where dept_no = '1';
>
> PG throws up:
> ERROR: Column 'dept_no' is ambiguous
>
> Is this not allowed? or is my sql understanding wrong?

It's correct behavior, because it can't differentiate between the two
fields with the same name. But this shouldn't present a problem if you
are using dept_no as a join field. You will want to select things this
way:

select d.*, e.empl_no, d.name_last from DEPARTMENT d, Employee e where
e.dept_no = d.dept_no and d.dept_no = 1;

It's longer SQL, but it's also more precise.

-- Brett
http://www.chapelperilous.net/~bmccoy/
------------------------------------------------------------------------
A bird in the hand is worth what it will bring.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message will trillich 2001-03-07 17:24:34 explain -> how to optimize?
Previous Message Richard Poole 2001-03-07 16:07:23 Re: ambiguous