Обсуждение: UNION or LEFT JOIN?

Поиск
Список
Период
Сортировка

UNION or LEFT JOIN?

От
Louis-David Mitterrand
Дата:
Here is the basic schema:
 -------------------------->id_ship>----------------------- |                                                        |
[SHIP]->id_ship->[CABIN]->id_cabin->[PRICE]<-id_cruise<-[CRUISE]

It's a database of cruise prices.

Each 'price' object has a reference to 'cabin' and 'cruise'

'cabin' belongs to a 'ship', so does 'cruise'

I'm trying to select all cabins of cruise N°1 with prices OR nothing if
there is no price (meaning cabin not available). I want all cabins
listed, price or no price.

Also when doing the query I don't have the id_ship, only the id_cruise.

What is the best way of doing it? UNION or LEFT JOIN? I tried the latter
without success and am unsure on how do do the former.

Thanks,


Re: UNION or LEFT JOIN?

От
Oliveiros
Дата:
Louis-David,

Please advice me,

if some cabin doesn't have a price i.e. it is not available, is there some way to tell
which cruise it belongs to?

You have PRICE table which seems to me to be an associative table between cruise and cabin, is this correct?

But, if the price doesn't have a register for that pair (îd_cabin,id_cruise), how do you know that cabin belongs to that cruise, in this case, cruise nº 1?

Best,
Oliveiros

2010/2/16 Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
Here is the basic schema:

 -------------------------->id_ship>-----------------------
 |                                                        |
[SHIP]->id_ship->[CABIN]->id_cabin->[PRICE]<-id_cruise<-[CRUISE]

It's a database of cruise prices.

Each 'price' object has a reference to 'cabin' and 'cruise'

'cabin' belongs to a 'ship', so does 'cruise'

I'm trying to select all cabins of cruise N°1 with prices OR nothing if
there is no price (meaning cabin not available). I want all cabins
listed, price or no price.

Also when doing the query I don't have the id_ship, only the id_cruise.

What is the best way of doing it? UNION or LEFT JOIN? I tried the latter
without success and am unsure on how do do the former.

Thanks,

--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

Re: UNION or LEFT JOIN?

От
Louis-David Mitterrand
Дата:
On Tue, Feb 16, 2010 at 03:33:23PM +0000, Oliveiros wrote:
> Louis-David,
> 
> Please advice me,

Hi Oliveiros,

> if some cabin doesn't have a price i.e. it is not available, is there some
> way to tell
> which cruise it belongs to?

In fact a cabin belongs to a ship and CAN be associated to a 'cruise'
event with a price(id_cruise,id_cabin) object.

> You have PRICE table which seems to me to be an associative table between
> cruise and cabin, is this correct?

Yes,

> But, if the price doesn't have a register for that pair
> (îd_cabin,id_cruise), how do you know that cabin belongs to that cruise, in
> this case, cruise nº 1?

I am trying to display a list of all cabins of a ship for a certain
cruise even if some prices are missing, so the user sees what cabins are
not available.

After much trial and error I was finally able to build a left join query
that works.

Thanks a lot for offering your help!


Re: UNION or LEFT JOIN?

От
Tim Landscheidt
Дата:
Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org> wrote:

> Here is the basic schema:

>   -------------------------->id_ship>-----------------------
>   |                                                        |
> [SHIP]->id_ship->[CABIN]->id_cabin->[PRICE]<-id_cruise<-[CRUISE]

> It's a database of cruise prices.

> Each 'price' object has a reference to 'cabin' and 'cruise'

> 'cabin' belongs to a 'ship', so does 'cruise'

> I'm trying to select all cabins of cruise N°1 with prices OR nothing if
> there is no price (meaning cabin not available). I want all cabins
> listed, price or no price.

> Also when doing the query I don't have the id_ship, only the id_cruise.

> What is the best way of doing it? UNION or LEFT JOIN? I tried the latter
> without success and am unsure on how do do the former.

Was does "without success" mean? The objective seems to be
straight-forward:

- Select all cabins that belong to the ship that belongs to the cruise id_cruise.
- Left join that with the prices of the cruise id_cruise.

Tim



Re: UNION or LEFT JOIN?

От
Louis-David Mitterrand
Дата:
On Tue, Feb 16, 2010 at 09:38:19PM +0000, Tim Landscheidt wrote:
> Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org> wrote:
> 
> > Here is the basic schema:
> 
> >   -------------------------->id_ship>-----------------------
> >   |                                                        |
> > [SHIP]->id_ship->[CABIN]->id_cabin->[PRICE]<-id_cruise<-[CRUISE]
> 
> > It's a database of cruise prices.
> 
> > Each 'price' object has a reference to 'cabin' and 'cruise'
> 
> > 'cabin' belongs to a 'ship', so does 'cruise'
> 
> > I'm trying to select all cabins of cruise N°1 with prices OR nothing if
> > there is no price (meaning cabin not available). I want all cabins
> > listed, price or no price.
> 
> > Also when doing the query I don't have the id_ship, only the id_cruise.
> 
> > What is the best way of doing it? UNION or LEFT JOIN? I tried the latter
> > without success and am unsure on how do do the former.
> 
> Was does "without success" mean? The objective seems to be
> straight-forward:
> 
> - Select all cabins that belong to the ship that belongs to
>   the cruise id_cruise.
> - Left join that with the prices of the cruise id_cruise.

Definitely the way to go. As the real schema is quite a bit more
complicated I was struggling with very long statements, but finally
succeded with a simple left join.

Thanks,