[SQL] UNION or LEFT JOIN?

2010-02-16 Thread 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,

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


Re: [SQL] UNION or LEFT JOIN?

2010-02-16 Thread 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

>

> 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 ([email protected])
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>


Re: [SQL] UNION or LEFT JOIN?

2010-02-16 Thread Louis-David Mitterrand
On Tue, Feb 16, 2010 at 03:33:23PM +, 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!

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


Re: [SQL] UNION or LEFT JOIN?

2010-02-16 Thread Tim Landscheidt
Louis-David Mitterrand  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


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


Re: [SQL] UNION or LEFT JOIN?

2010-02-16 Thread Louis-David Mitterrand
On Tue, Feb 16, 2010 at 09:38:19PM +, Tim Landscheidt wrote:
> Louis-David Mitterrand  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,

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


[SQL] very frustrating feature-bug

2010-02-16 Thread silly sad


acc=>

CREATE OR REPLACE FUNCTION add_user (TEXT, TEXT, TEXT, TEXT)
RETURNS usr AS $$
  INSERT INTO usr (login,pass,name,email) VALUES ($1,$2,$3,$4)
  RETURNING usr.*;
$$ LANGUAGE sql SECURITY DEFINER;

acc=>

ERROR:  return type mismatch in function declared to return usr
DETAIL:  Function's final statement must be a SELECT.
CONTEXT:  SQL function "add_user"

SURPRISE :-) SURPRISE :-)

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