Hi Peter,
>SELECT IF(col1=3, (Select col2 from table2 where table2.id = 1), (Select
>col2 from table3 where table3.id = 1)) FROM table1 WHERE id=1;
Thanks you for you answers.
Can I put SELECT statements inside the IF statement?
Mysql give error
ERROR 1064: Error in SQL syntax.
Under Control Flow functions of the manual, it says
IF(expr1,expr2,expr3)
If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2,
else it returns expr3. IF() returns a numeric or string value, depending on the
context in which it is used.
But doesn't say what expr is. Examples only show numeric and string functions.
-----Original Message-----
From: Peter Lauri [mailto:[EMAIL PROTECTED]
Sent: Wednesday, June 21, 2006 4:43 PM
To: [EMAIL PROTECTED]; Song Ken Vern-E11804
Cc: [email protected]
Subject: RE: if else statement
SELECT IF(col1=3, (Select col2 from table2 where table2.id = 1), (Select
col2 from table3 where table3.id = 1)) FROM table1 WHERE id=1;
That should do it.
-----Original Message-----
From: Thomas Lundström [mailto:[EMAIL PROTECTED]
Sent: Wednesday, June 21, 2006 7:51 PM
To: Song Ken Vern-E11804
Cc: [email protected]
Subject: Re: if else statement
Not sure what you're aming for here and how your data is structured but why not
use a join and alias and fetch all info in one select and then solve what you
need in your code?
Something in the line of:
select t2.col2 from_t2, t3.col2 from_t3 from table1 t1, table2 t2, table3 t3
where t1.id = t2.id
and t1.id = t3.id
and t1.id = 3
Maybe you can do something like that?
Regards,
Thomas L.
ons 2006-06-21 klockan 17:16 +0800 skrev Song Ken Vern-E11804:
> 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?
>
> Thanks
>
> --
> Ken
> e11804
>
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/[EMAIL PROTECTED]
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/[EMAIL PROTECTED]