Thread: 2 tables, joins and same name...

2 tables, joins and same name...

From
Marc André Paquin
Date:
Hello,

Here is 2 tables:

airport
---------
airport_id
name
code
city_id

destination
-----------
destination_id
dest_name
...
airport_dep_id  // using airport.airport_id (departure)
airport_arr_id  // using airport.airport_id  has well (arrival)

I have 2 columns in the second table that uses the same name column in
the first table...

I dont know how to formulate my SQL query... I want to select the
destinations in the destination table with not the ID of each airport
but their names. I can do a join with one but with the second one, I get
no results... And this is confusing!

select dest.dest_name, air.name as airport1, air.name as airport2 from
destination, airport air where dest.airport_dep_id_id=air.airport_id and
dest.airport_arr_id=air.airport_id;

This is not good...

Any help?

Thanks!
--
Marc Andre Paquin

Re: 2 tables, joins and same name...

From
Jeff Davis
Date:
Whew... a little complex. I think I know what you are trying to do.

First of all, try looking at the docs at the postgres site for UNION and also
for INTERSECT (both are found in "reference/sql commands/SELECT"). You might
not need either of these; your question isn't clear about what you want to do.

Maybe you just need to use OR instead of AND in your SQL. The SQL below
doesn't even look valid to me...

Try showing example table data and expected results (or maybe a simplified
version?). It is not clear exactly what results you are expecting for what
criteria you specify. It might be possible to simplify quite a bit.

Regards,
Jeff Davis

On Thursday 30 August 2001 01:26 pm, you wrote:
> Hello,
>
> Here is 2 tables:
>
> airport
> ---------
> airport_id
> name
> code
> city_id
>
> destination
> -----------
> destination_id
> dest_name
> ...
> airport_dep_id  // using airport.airport_id (departure)
> airport_arr_id  // using airport.airport_id  has well (arrival)
>
> I have 2 columns in the second table that uses the same name column in
> the first table...
>
> I dont know how to formulate my SQL query... I want to select the
> destinations in the destination table with not the ID of each airport
> but their names. I can do a join with one but with the second one, I get
> no results... And this is confusing!
>
> select dest.dest_name, air.name as airport1, air.name as airport2 from
> destination, airport air where dest.airport_dep_id_id=air.airport_id and
> dest.airport_arr_id=air.airport_id;
>
> This is not good...
>
> Any help?
>
> Thanks!

Re: 2 tables, joins and same name...

From
Alvaro Herrera
Date:
On Fri, 31 Aug 2001, Jeff Davis wrote:

> Whew... a little complex. I think I know what you are trying to do.

What's wrong with something like

select airport1.name as departure, airport2.name as arrival from
destination, airport as airport1, airport as airport2 where
airport1.airport_id=airport_dep_id and
airport2.airport_id=airport_arr_id;

?

Maybe I'm overlooking something, but it seems pretty straightforward to
me.

--
Alvaro Herrera (<alvherre[@]atentus.com>)