Mysql – Get the rows where id_user_rq exists in id_user_ap and viceversa

MySQLquery

I have this table structure: http://www.dropmocks.com/mBjNvF and need to get the id_user_rq if not exists in id_user_ap as the title said. I need to get the rows where id_user_rq = 1 and id_user_ap = 2 and id_user_rq = 2 and id_user_ap = 1 meaning that I send a invitation and the other part accept me and inverse the relation, any help?

Best Answer

Please try this:

SELECT A.* FROM
(SELECT * FROM default_relation_users WHERE id_user_rq = 1) A
LEFT JOIN
(SELECT * FROM default_relation_users WHERE id_user_ap = 1) B
ON
(A.id_user_rq=B.id_user_ap AND B.id_user_rq=A.id_user_ap)
WHERE B.id_user_ap IS NOT NULL;