I have a Mysql junction table user_connections which maps the users table with the following columns: user_from and user_to, both are foreign keys in users table.
My logic is if id1 sends a request to id2, in this case, that will be a pending request. The request will be considered aprroved only if id2 accepts the request which will give the pattern above of (id1, id2) and (id2, id1) in the table (red box).
So my question is how can I query the user_connections table so I can get all the pending requests based on id1 (blue box)
I have no idea how I can acheave this. So any help will be appriciated. Thank you in advance.


If you are looking for all the pending requests defined by your
user_connectionstable, then you need to do a left outer join of the table with itself as follows:Schema (MySQL v5.7)
Query #1
View on DB Fiddle
For every (id1, id2) in the table we attempt to match it with a row with values (id2, id1). If there is no match, which is the case for pending requests, then the
uc2.user_fromanduc2.user_tocolumns will be null and so we just select from the left outer join those rows whereuc2.from_useris null.I created the db-fiddle for you, but this something that you should have done yourself.