Actually, I am interested in how to reduce and optimize this query:

SELECT IF(friends.id1 = '$id', friends.id2, friends.id1) id, profiles.username FROM friends, profiles WHERE (friends.id1 = '$id' OR friends.id2 = '$id') AND (friends.id1 = OR friends.id2 = ) AND NOT(id = '$id') ORDER BY id 

    2 answers 2

    First of all, it needs a little tweaking on the merits. The condition in the WHERE is incorrect.

    It should be wrong:

      (friends.id1 = '$id' OR friends.id2 = '$id') AND (friends.id1 = OR friends.id2 = ) 

    And so:

      (friends.id1 = '$id' AND friends.id2 = ) OR (friends.id2 = '$id' AND friends.id1 = ) 
    • It should be OR :) - ka5itoshka
    • Believe it! It should be exactly as I wrote. OR is there too, but in a different place. - Shamov
    • @Shamov, I apologize)) I did not notice)) - ka5itoshka

    Here is the code section

     AND NOT(id = '$id') 

    can be replaced by

     AND id != '$id'