pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: D'Arcy J.M. Cain <darcy@druid.net>
To: Kenneth Gonsalves <lawgon@thenilgiris.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: sql can i substitute
Date: Fri, 17 Dec 2004 07:42:57 -0500
Message-ID: <20041217074257.42a44460.darcy@druid.net> (raw)
In-Reply-To: <0412171655451H.06834@thenilgiris.com>
References: <0412171655451H.06834@thenilgiris.com>

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.



view thread (10+ messages)  latest in thread

Message-ID: <20041217074257.42a44460.darcy@druid.net>
Permalink:  ../20041217074257.42a44460.darcy@druid.net/
Also on:    postgresql.org/message-id/20041217074257.42a44460.darcy@druid.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: darcy@druid.net, lawgon@thenilgiris.com
  Subject: Re: sql can i substitute
  In-Reply-To: <20041217074257.42a44460.darcy@druid.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox