Canman2005 Posted June 21, 2008 Share Posted June 21, 2008 Hi all Wondering if anyone can help. Basically I have a `members` table which looks like `MEMBERS` - TABLE ID NAME MYID 1 John 11 2 Dave 2 3 Emma 11 4 Sarah 11 5 Steve 8 What I do then is run a QUERY on this table to get all the members which belong to me (I am ID number 11), this would be done with a QUERY like SELECT * FROM `members` WHERE `myid` = 11 that would get the following rows back from the `members` table `MEMBERS` - TABLE ID NAME MYID 1 John 11 3 Emma 11 4 Sarah 11 This shows that ID numbers 1 / 3 / 4 belong to me as my member ID is stored on thier row. What I want to do is run another QUERY on a table called `posts` which gets all the rows which belong to those members returned from the `members` table. The posts table looks like `POSTS` - TABLE ID TITLE MEMBERID 1 Hello 2 2 Holiday 4 3 Travel 9 4 Boats 4 5 Planes 6 So when I run the QUERY i'm trying to figure out, it would basically return the following rows `POSTS` - TABLE ID TITLE MEMBERID 2 Holiday 4 3 Travel 3 4 Boats 4 because the `member` id's that belong to me (1 / 3 / 4) are contained the the `memberid` field in the `posts` table. Does this make any kind of sense to anyone? Can anyone help me? Been up for 7 hours trying to do this, but no where fast Any help would be ace Thanks in advance Ed Quote Link to comment Share on other sites More sharing options...
Mattyspatty Posted June 21, 2008 Share Posted June 21, 2008 you can run an SQL query that should achieve all that for you using http://dev.mysql.com/doc/refman/5.0/en/join.html. its been a while since ive used it so try asking in the MySQL forums. Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted June 21, 2008 Share Posted June 21, 2008 SELECT m.name, p.title FROM members m LEFT JOIN posts p ON m.id = p.memberid As Matty said, learn about joins...this is as basic as they come. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted June 21, 2008 Share Posted June 21, 2008 Your setup confuses me... when you put `POSTS` - TABLE ID TITLE MEMBERID 2 Holiday 4 3 Travel 3 4 Boats 4 but in the table above it, travel's memberid was 9. Also, you said you're looking for 1/3/4(in your example) and you have 4/3/4 listed as the results you expect to get. Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted June 22, 2008 Author Share Posted June 22, 2008 thanks guys, think I cracked it Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.