On Wednesday 21 June 2006 11:16, Song Ken Vern-E11804 wrote:
> Hi,
>
> I'm trying to build a query in using SQL instead of doing it in Perl.
>
> I am trying to do something like this :
>
> If ((select col1 from table1 where id = 1) == 3)
> Then
> Select col2 from table2 where table2.id = 1;
> Else
> Select col2 from table3 where table3.id = 1;
>
> In Perl I would probably do have to access the DB twice.
>
> Select col2 from table1 where if = 1;
>
> If (col2 == 3) {
>  Select col2 from table2 where table2.id = 1;
> } else {
>  Select col2 from table3 where table3.id = 1;
> }
>
> I've read the manual on subqueries but the example don't indicate how I
> can
> do a conditional test using a subquery?
>
> Am I on the right track or is there another way to do this?

Maybe:

(SELECT col2 FROM table2 left join table1 on table1.id=1 where table1.col1=3 
and table2.id=1)
UNION
(SELECT col2 FROM table3 left join table1 on table1.id=1 where table1.col1<>3 
and table3.id=1);

I have not tested it...

-- 
Jørn Dahl-Stamnes
homepage: http://www.dahl-stamnes.net/dahls/

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to