postgresql7.1 select (sn.user_id) as user_id_cur, (sn.im_screen_name) as jabber_screen from jb_screens sn where (sn.user_id = :user_id or sn.user_id = :user_id_friend) and sn.service = 'jabber' select screen_id from jb_screens where user_id = :friend_id insert into jb_friends (user_id , friend_screen_id) values (:user_id , :screen_id) UPDATE jb_screens set refcount = (refcount +1) where screen_id = :screen_id