Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Storing 'friends' for user in SQL

  • 23-09-2008 01:24PM
    #1
    Registered Users, Registered Users 2 Posts: 8,070 ✭✭✭


    need to store friends for a user in an SQL, database.
    How would this be possible, without me having to store them in one field with some sort of character divider. Also limit would be varchar, 255.

    This would then need to be populated somewhere else.
    Need to implement something like this,
    http://lesterchan.net/wordpress/2006/07/05/donations/email/

    But where they can select their friends !
    Thanks.


    edit: i actually just a realised i could store friends details, in a different table associated with the user id - DOH


Comments

  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    A normal many:many relationship.

    What you do is have one table with two columns - the user's id and the friend's id.

    Call it "userfriend" or something.

    When you want to get a list of friend ids for a particular person, you just call

    SELECT friendid FROM userfriend WHERE userid = 10

    Or you can go further and do a join, and get a list of all their friends' details:

    SELECT u.* from user u, userfriend uf
    WHERE u.userid = uf.friendid
    AND uf.userid = 10

    The table looks something like this:
    userid | friendid
    10| 1
    10| 3
    10| 4
    10| 6
    10| 8
    4| 9
    4| 1
    4| 6
    9| 1


  • Registered Users, Registered Users 2 Posts: 8,070 ✭✭✭Placebo


    thanks seamus, i just realised myself. Il just make a new table and go that easy route


  • Registered Users, Registered Users 2 Posts: 1,266 ✭✭✭Overflow


    seamus wrote: »
    A normal many:many relationship.

    What you do is have one table with two columns - the user's id and the friend's id.

    Call it "userfriend" or something.

    When you want to get a list of friend ids for a particular person, you just call

    SELECT friendid FROM userfriend WHERE userid = 10

    Or you can go further and do a join, and get a list of all their friends' details:

    SELECT u.* from user u, userfriend uf
    WHERE u.userid = uf.friendid
    AND uf.userid = 10

    The table looks something like this:
    userid | friendid
    10| 1
    10| 3
    10| 4
    10| 6
    10| 8
    4| 9
    4| 1
    4| 6
    9| 1

    I concur, that would be the way to do it!


Advertisement