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.