Selecting multiple table with single foreign key?

How to get all records from multiple tables using single foreign key?

i.e
i have five tables named students,personal details,qualification,academics,fees and each of those has a foreign key name stu_id from students table.Now i want to fetch all records of particular stu_id from all tables?

This is an SQL question more so than a PHP one, you need to read up on JOIN. So for example in a discussion board you might do something like

select * from threads inner join posters on threads.poster_id = posters.id

Obviously you wouldn’t usually use * and instead just retrieve the columns that you need, but that’s the way I’d look at it. The above query would retrieve all discussion threads, and as well as retrieving the poster_id in the “main” table, it would also retrieve the poster details for each poster, to display name, avatar and so on.

So in your case if you were displaying fees and wanted to get the student name, you might do something like

select * from fees inner join students on fees.stu_id = students.id

In that case, for each fee row that comes back, it will contain the appropriate information from the student table. The “on” clause tells the query how to select the information from the other table.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.