xrcwrn
xrcwrn

Reputation: 5327

Query for fetching data from multiple tables

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

Upvotes: 0

Views: 234

Answers (1)

8bitcat
8bitcat

Reputation: 2234

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

Upvotes: 1

Related Questions