Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2011
    Posts
    1

    Unanswered: Can someone help me write a query for many-to-many relationship please?

    Hi guys!

    I'm new to this forum and was wondering if someone can help me out. I know how to write simple queries but having trouble with this:

    Assume I have the following Tables

    -------------
    user
    -------------
    userid
    name
    picture
    etc.

    -------------
    group
    -------------
    groupid
    etc.


    -------------
    group_has_user
    -------------
    groupid
    userid


    Let's say I would like to Get array with userid, user.name, and user.picture for all users who are member of groupid 5

    How would I write such a query?

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by smartcan View Post
    Let's say I would like to Get array with userid, user.name, and user.picture for all users who are member of groupid 5
    Code:
    SELECT user.userid
         , user.name
         , user.picture
         , user.etc
      FROM group_has_user
    INNER
      JOIN user
        ON user.userid = group_has_user.userid
     WHERE group_has_user.groupid = 5
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •