How can i get all the values using vise versa column from database in mySql

Samir Sheikh :

I want to get data using vise versa column from database in mySql Database.

For example, see following image. enter image description here

Above image you can see column name initiator_user_id & friend_user_id column , So I want all the those friends_user_id and initiator_user_id that are in stored in both column. So here 1 is stored in both column so i want 89,63,155 ids that are stored in both column.

I have tried to get record using CASE Statement but it'll not work.

SQL Query..

 SELECT *,
CASE
    WHEN friend_user_id != '' THEN initiator_user_id 
    WHEN initiator_user_id != '' THEN friend_user_id 
    ELSE ''
END AS users_id
FROM tablename

Someone please let me know , How could i get those records?

Aaron W. :

Handful of ways this could be done - I quickly threw together a UNION DISTINCT statement on sqlfiddle that may help. Essentially it's just taking two queries and putting them together to get you your results.

(SELECT initiator_user_id as user_id FROM test WHERE friend_user_id = 1) 
UNION DISTINCT 
(SELECT friend_user_id as user_id FROM test WHERE initiator_user_id = 1)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=372557&siteId=1
Recommended