+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: SQL help (possible a join)

  1. #1
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    SQL help (possible a join)

    I have two tables:

    feed:
    - id
    - user_id
    - content

    friends:
    - id
    - user_id
    - user2_id

    Say (for example) that friends contains the following data:

    0, 1, 4
    1, 1, 6
    2, 1, 12
    3, 6, 1
    3, 4, 1
    3, 1, 15
    3, 12, 6

    feed contains the following data:

    0, 1, 'Lorem Ipsum'
    1, 2, 'Hello world!'
    2, 1, 'What?'
    3, 4, 'Bla'
    4, 7, 'Test'

    I want to get all items from the feed that are from friends. For example, if the current user id is 1, I want to get all the feed info from users 4, 6, 12 and 15 (and themselves):

    0, 1, 'Lorem Ipsum'
    2, 1, 'What?'
    3, 4, 'Bla'


    How would I do this using SQL? It should be portable SQL, usable in MySQL, Firebird etc.

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  2. #2
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: SQL help (possible a join)

    Hi callumacrae,


    I would run 2 separate queries. The first one will get the user results:

    Code:
    SELECT id, user_id, content
    FROM feed
    WHERE feed.user_id = ?
    And the second one for the user's friends:

    Code:
    SELECT feed.id,
           feed.user_id,
           feed.content
    FROM feed
    INNER JOIN friends
    ON friends.user2_id = feed.user_id
    WHERE friends.user_id = ?
    ORDER BY feed.id ASC
    LIMIT 10
    Save both of the results into an array and there you go.

  3. #3
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: SQL help (possible a join)

    Can I do an OR on the inner join?

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: SQL help (possible a join)

    Code:
    SELECT feed.id , feed.user_id, feed.content 
        FROM feed INNER JOIN friends 
        ON feed.user_id = friends.user2_id  
        WHERE friends.user_id = current_user ;
    should work

    Edit: facepalm.png ... thirty lashes with a wet lasagna noodle for me for not reading gomarc's post more closely.
    Last edited by descalzo; 04-14-2011 at 11:58 AM. Reason: caffiene deprivation
    Nothing is always absolutely so.

  5. #5
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: SQL help (possible a join)

    Quote Originally Posted by gomarc View Post
    Code:
    SELECT feed.id,
           feed.user_id,
           feed.content
    FROM feed
    INNER JOIN friends
    ON friends.user2_id = feed.user_id
    WHERE friends.user_id = ?
    ORDER BY feed.id ASC
    LIMIT 10
    :/

    Well this is awkward...

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  6. #6
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: SQL help (possible a join)

    Quote Originally Posted by callumacrae View Post
    Can I do an OR on the inner join?

    ~Callum
    Yes you can use the SQL UNION operator to combine the results of two queries into a composite result.

    It will be something like this:

    Code:
    SELECT feed.id, feed.user_id, feed.content
    FROM feed
    WHERE feed.user_id = ?
    UNION
    SELECT feed.id, feed.user_id, feed.content
    FROM feed
    INNER JOIN friends ON friends.user2_id = feed.user_id
    WHERE friends.user_id = ?

  7. #7
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: SQL help (possible a join)

    Will that return them in proper order or will it be all the users stuff and then their friends stuff?

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  8. #8
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: SQL help (possible a join)

    Will that return them in proper order or will it be all the users stuff and then their friends stuff?

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  9. #9
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: SQL help (possible a join)

    callumacrae,

    As posted, the query has no defined order. You need to add 'ORDER BY'.

    If you want it by feed.id ASC:

    Code:
    SELECT feed.id, feed.user_id, feed.content
    FROM feed
    WHERE feed.user_id = ?
    UNION
    SELECT feed.id, feed.user_id, feed.content
    FROM feed
    INNER JOIN friends ON friends.user2_id = feed.user_id
    WHERE friends.user_id = ?
    ORDER BY id ASC

  10. #10
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: SQL help (possible a join)

    Will that affect the entire union or just the second statement?
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Join my forum and i will join your site
    By Daniel S in forum The Marketplace
    Replies: 4
    Last Post: 02-02-2008, 05:42 PM
  2. Join my forums and ill join yours!
    By Legostylist in forum The Marketplace
    Replies: 2
    Last Post: 01-04-2008, 04:31 PM
  3. how do i join??
    By phaos in forum Free Hosting
    Replies: 2
    Last Post: 01-27-2007, 10:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers