Re: sql can i substitute - Mailing list pgsql-sql

From D'Arcy J.M. Cain
Subject Re: sql can i substitute
Date
Msg-id 20041217074257.42a44460.darcy@druid.net
Whole thread Raw
In response to sql can i substitute  (Kenneth Gonsalves <lawgon@thenilgiris.com>)
Responses Re: sql can i substitute
List pgsql-sql
On Fri, 17 Dec 2004 16:55:45 +0530
Kenneth Gonsalves <lawgon@thenilgiris.com> wrote:
> i want to write an sql statement like this:
> 
> select fruit from table
> 
> which should return 'good' if fruit = 1 and 'bad' if fruit =2 and
> 'rotten' if fruit =3

An alternative to Andreas' suggestion would be to create a simple lookup
table and join them.  This is good if the real life example can get
larger and/or the list can change and you don't want to modify code
every time it does.  

[totally made up output]
fstate_id | fstate_name
----------+-------------       1 | good       2 | bad       3 | rotten

SELECT fstate.fstate_name AS "Fruit state"
FROM table, fstate
WHERE table.fstate_id = fstate.fstate_id;

Now you can easily add another state:

INSERT INTO fstate VALUES (4, 'smelly');

-- 
D'Arcy J.M. Cain <darcy@druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.


pgsql-sql by date:

Previous
From: Richard Huxton
Date:
Subject: Re: can't get the order I want after inserting new rows
Next
From: Kenneth Gonsalves
Date:
Subject: Re: sql can i substitute