OT : MySQL statement question

Tom Metro tmetro-blu-5a1Jt6qxUNc at public.gmane.org
Mon Nov 29 20:04:36 EST 2010


tbs wrote:
> What I was wondering is, can I do this :
> 
> select t.a, t.b, t.c, t.d
> from table 't'
> where
> ta. = 'foo'
> and t.b = 'bar'
> IF (cond) THEN
>     AND isNULL(t.c)
> ELSE
>     AND t.c in(vINPUTLIST)
> ENDIF;

It should be possible to avoid repeating the long SQL statement twice
using one of two possible techniques:

1. Write your WHERE clause using IF() or other functions (functions, not
flow control statements, as you show above) such that it always returns
true when the supplied vINPUTLIST is NULL.

2. Build your SQL as a string, as illustrated in this thread:
http://forums.mysql.com/read.php?98,395690,395690#msg-395690
This is much as you would do in an application language, and in this
case you can use flow control statements. Though this approach might
subvert some of MySQL's optimizations.


> well I have googled until my eyes are bleeding but cannot get a
> definitive answer on this one ...

Try the MySQL Stored Procedures Forum:
http://forums.mysql.com/list.php?98

 -Tom

-- 
Tom Metro
Venture Logic, Newton, MA, USA
"Enterprise solutions through open source."
Professional Profile: http://tmetro.venturelogic.com/





More information about the Discuss mailing list