You are here: Re: MySQL Select Case « PHP Programming Language « IT news, forums, messages
Re: MySQL Select Case

Posted by rlee0001 on 06/12/06 06:48

vncntj@hotmail.com wrote:
> I'm trying to get the venue in the 'List' column when in contains a
> value. But if the venue Is Null then place the event in the 'List'
>
> SELECT
> CASE
> WHEN event Is Null THEN venue
> Else
> event
> End as 'List'
> ,DATE_FORMAT(fld_date, '%M') as Monthly
> >From na_statistics

SELECT
COALESCE("event", "venue") AS "list",
DATE_FORMAT("date", '%M') AS "monthly"
FROM "my_namespace"."na_statistics";

So why not use the COALESCE function? Is this not exactly what it is
for? You can have as many values as you want and the database with use
the first non-null value specified. But that said I see nothing wrong
with your syntax for CASE except that the logic does not match your
explaination (you say you want "venue" if available and "event" only
when "venue" isn't; but you are instead checking "event" for null and
using "venue" only then). Also, IF/THEN could have done the same work
as well. Or "IFNULL" (similar to COALESCE but accepting exactly two
parameters).

And just an aside: I'm not sure about MySQL but under PostgreSQL you
_can_ use reserved words in field names as long as they are surrounded
by double-quotes. So fld_date can become just "date" which I think is a
bit more elegant. Besides that I always surround my identifiers in
quotes out of habit. :o) The only thing with that is that when you put
identifiers in quotes IIRC they become case sensitive so I also make it
a habit to use lower case for identifiers all the time. And I guess
other than that it's a good idea to fully qualify table names with the
appropriate namespace. In MySQL that means always specifying the
database in the FROM/JOIN clauses. In PostgreSQL that means always
specifying the schema there.

-Robert

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация