Forum Moderators: open

Message Too Old, No Replies

controlling the select statement?

         

maliskoleather

6:05 am on Feb 13, 2007 (gmt 0)

10+ Year Member



Im not too sure if this is truly possible, but I'm trying to find a way to control the field selected, based of the value of another field... for example:

(SELECT friend_id AS friend WHERE user_id='1') OR (SELECT user_id AS friend WHERE friend_id='1' AND mutual='y')

I know this dosent work as-is, and yes, i could just run two seperate queries, then join the results together, but i'd like to avoid repeating the code... I'm already making several large queries on this script, and would like to avoid any more...

any help would be greatly appreciated

syber

1:02 pm on Feb 13, 2007 (gmt 0)

10+ Year Member



You could use the CASE statement


SELECT CASE
WHEN friend_id = '1' AND mutual = 'y'
THEN user_id
WHEN user_id = '1'
THEN friend_id
ELSE null
END CASE AS friend
FROM table

I included the ELSE clause to point out that you should allow for neither being true.