I am coding a friendship system and it has two tables.
members
- id
- username
- password
friends
- id
- user_id
- friend_id
- status
Let’s say that i want a query that can select the friends IDs of the member $userId how possible to make this in one query?
I found a solution which is to make 2 queries. The fist selects the friends WHERE user_id = $userId AND the second selects friends WHERE friend_id = $userId and then MIX them in one array. If there is no other solution I’m going to use it.
please any ideas for both the SQL structure & Queries?
Use:
Using
UNIONwill remove duplicates.UNION ALLwould be faster, but it doesn’t remove duplicates.If you want to get the information for the friends from the
MEMBERStable, use:BTW: I hope you’re using mysql_escape_string on the variables, otherwise you risk SQL injection attacks:
