doddsey_65 Posted September 12, 2011 Share Posted September 12, 2011 For my friend system usernames are stored in a table (f_username) along with the username of the friend (f_friend_username). Im trying to display the friends of a user on their profile but the users username can be in either the f_username column or the f_friend_username column. So i have this query but it returns the same result twice. Is there a better way around this? I thought about a conditional ON statement but don't know if it's possible. $sql = "SELECT f.*, u.u_avatar FROM ".Asf_Db::$prefix."friends f JOIN ".Asf_Db::$prefix."users u ON ( f.f_username = u.u_username OR f.f_friend_username = u.u_username ) WHERE ( f_username = ? OR f_friend_username = ? ) AND f_confirmed = ?"; $sth = Asf_Core::$db->prepare($sql); $sth->execute(array( $username, $username, 1 ) ) or die(Asf_Core::$db->error($sth, $sql)); Quote Link to comment Share on other sites More sharing options...
requinix Posted September 12, 2011 Share Posted September 12, 2011 Just stick the WHERE conditions into the JOIN's: SELECT f.*, u.u_avatar FROM friends f JOIN users u ON ( f.f_username = u.u_username AND f.f_friend_username = $username OR f.f_friend_username = u.u_username AND f.f_username = $username ) AND f.f_confirmed = 1 You might want a DISTINCT too if your friendslisting thing doesn't already check for an existing pair when adding friends. Quote Link to comment Share on other sites More sharing options...
fenway Posted September 12, 2011 Share Posted September 12, 2011 Yes, but use parenthesis or your ORs and ANDs won't work. Quote Link to comment Share on other sites More sharing options...
requinix Posted September 12, 2011 Share Posted September 12, 2011 Yes, but use parenthesis or your ORs and ANDs won't work. :-\ But ANDs are higher than ORs, so the way I have it should be fine. And for the record the f_confirmed thing was supposed to be in a WHERE, but I suppose it's okay where it is. Quote Link to comment Share on other sites More sharing options...
fenway Posted September 12, 2011 Share Posted September 12, 2011 Yes, but use parenthesis or your ORs and ANDs won't work. :-\ But ANDs are higher than ORs, so the way I have it should be fine. I sincerely hope you don't rely on the arbitrary order-of-operations that way -- you should be able to add an arbitrary new expression without breaking everything. Add the 4 characters, you'll thank me later. Order-of-operations shouldn't exist. Quote Link to comment Share on other sites More sharing options...
Pandemikk Posted September 12, 2011 Share Posted September 12, 2011 Fenway has a point, it can be a headache to alter your existing query and spend hours looking for why the results aren't showing up as expected only to realize its because of that pesky new AND / OR clause messing up your logic. I speak from personal less-than-a-week-ago experience. It also looks nicer. Nicer looking code = Super A+ bro guy; Quote Link to comment Share on other sites More sharing options...
requinix Posted September 13, 2011 Share Posted September 13, 2011 Fair enough, but I always write sets of conditions in the same way that makes it easy for me to tell where my ANDs and ORs are. Believe me, if I thought there would be ambiguity then I would be more explicit. (cond OR cond) AND cond AND (cond OR cond) cond AND cond AND cond OR cond AND cond AND cond OR cond AND cond AND cond But to each their own. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.