Re: sql can i substitute

From: "D'Arcy J(dot)M(dot) Cain" <darcy(at)druid(dot)net>
To: Kenneth Gonsalves <lawgon(at)thenilgiris(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: sql can i substitute
Date: 2004-12-17 12:42:57
Message-ID: 20041217074257.42a44460.darcy@druid.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Fri, 17 Dec 2004 16:55:45 +0530
Kenneth Gonsalves <lawgon(at)thenilgiris(dot)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(at)druid(dot)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.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Kenneth Gonsalves 2004-12-17 12:52:48 Re: sql can i substitute
Previous Message Richard Huxton 2004-12-17 12:37:05 Re: can't get the order I want after inserting new rows