在单个查询中从两个不同的表中获取详细信息

在单个查询中从两个不同的表中获取详细信息

问题描述:

I have a table called project and another table student. I want to fetch all the details about the project and all the details of the student in that project from single query. student table has project id as foreign key.There can be many students in a project.

我有一个名为project的表和另一个表student。 我想从单个查询中获取有关项目的所有详细信息以及该项目中学生的所有详细信息。 学生表将项目ID作为外键。项目中可以有很多学生。 p> div>

Select p.*,s.*(for all columns) from project p, Student s 
inner join p.id=s.id

Use a INNER JOIN and join both the table like below. That's basic set but you will be able to modify the SELECT column list per your need.

select s.* 
from student s
join project p on p.id = s.project_id;

You can use JOIN for fetch data from both table using foreign key. So for this your query become like this

SELECT st.*,pr.* 
FROM student st
JOIN project pr ON pr.id = st.project_id;

I hope you will get solution.