0

I am having following tables

 spam (element_id, spam_table,spam_by,add_date)

here (element_id,spam_by) is primary key and spam_by is foreign key depends on id of user

user(id,fname,lname)

id is primary key

profile_pic(pic_id,profile_p,userid)

userid is foreign key depends on id of user

comments( comment_id, comment_on, commented_by, comment_date, comment)

comments_id is primary key, comment_on is foreign key depends on id of elements commented_by foreign key depends on id of user

 elements(id,name)

id is primary key

from the above table. I want to fetch following details from above tables

 commented userid, name,pic,comment,commemt on element,spam userid, name,pic
2
  • Please check your wanted detail columns, they don't correspond to the tables you specified.."commented", or "pic" is in no table Commented Dec 29, 2012 at 9:31
  • what is commented, is that your new table ?? Commented Dec 29, 2012 at 9:39

1 Answer 1

1

Start to look into join and join tables on userid.

 SELECT data you want
 FROM first table to join
 INNER JOIN secondtable
 ON first table userid = secondtable userid JOIN third table
 ON first table userid = thirdtable userid
 JOIN fourth table
 ON first table userid = fourth table userid

And so on until included everything you need. DONT forget to give each table an alias

It is done like firsttable alias You then query your table like this alias.userid

Sign up to request clarification or add additional context in comments.

4 Comments

There are many answer telling people where to start?! Anyeay edited my answer formatting is due to the fact I editing from my cell phone.
@Suggestion should be kept put in Comments, though, anyway removed my -1 and added a +1 though, for your complete answer
Thanks Sashi! And your right, I will keep suggestions in comments from now on :)
this is general answer which will work for fetching data from multiple tables

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.