If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Can someone help me write a query for many-to-many relationship please?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-30-11, 17:39
smartcan smartcan is offline
Registered User
 
Join Date: Sep 2011
Posts: 1
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?
Reply With Quote
  #2 (permalink)  
Old 09-30-11, 20:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Tags
mysql

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On