Raouf Athar Raouf Athar - 1 year ago 123
MySQL Question

MySql Query to get friend list from Friends table

I have a table named friends with following structure:

id, from_id, to_id, confirmed, ....

This table contains the list of all friends of all users. The from_id and to_id contain ids of the users where from_id is the one who actually sent the friend request.

Now, I need to select the list of all the friends of a particular user into.

Is is better to do it using the following query or a query with union:

"select (from_id + to_id)- $user_id as friend_id, ... from friends where from_id=$user_id or to_id=$user_id"

Answer Source

Although I love your mathematic-fan solution I think it is not considering overflow issues. For example: what would happen if you add two extremely big numbers?

Apart from that, your solution relies on a numeric user_id. Using a union might take a bit more (you should test that to make sure) but will work with any data type.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download