How to show list of the others who have relationship with the user

Discussion in 'MySQL' started by ketting00, Jul 8, 2016.

  1. #1
    Hi guys,

    I'm trying to show list of the other users who have relationship with the user, I just happened to got dumb for a while. So how to do this.

    Here's my simple table:
    
    TB1
    tb1_id    tb1_name
    1        Red
    2        Blue
    3        Yellow
    
    TB2
    tb2_id    tb2_id1    tb2_id2    tb2_type    tb2_value
    1          3          1          A              2
    
    Code (markup):
    I want to show every other users (in random if large number) on the list. Here's what I've tried so far:
    
    SELECT
        tb2_id1, tb2_id2, tb2_type, tb2_value, tb1_id, tb1_name
    FROM
        TB2
    LEFT JOIN
        TB1
        ON
            tb2_id1 = tb1_id OR tb2_id2 = tb1_id
    WHERE
        (tb2_id1 != :user_id OR tb2_id2 = :user_id) AND tb2_type = :tb2_type AND tb2_value = :tb2_value AND RAND()<(SELECT ((15/COUNT(*))*10) FROM TB2)
    ORDER BY
        RAND()
    LIMIT
        15
    
    Code (markup):
    In this case, my user id is 3.
    Thanks in advance,
     
    ketting00, Jul 8, 2016 IP
  2. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128