This question already has an answer here:
Ok, so I at the moment, I have one table "project" with Id and other stuff. I need another table that will hold subprojects of the first table. How would I be able to connect these two tables so that when the information from the first table is accessed, it will also bring the subproject from the other table with it. Example:
______
|1|John|
|2|Bob |
Project Table
___________
|1|DO WORK |
|2|DONT WORK|
SubProject Table
When I click view info button the table should print out all the info from project as well as his subproject b/c of the same ID
</div>
Just add a foreign key to the second table and name it project_id
, for example:
___________ ___
|1|DO WORK | 1 |
|2|DONT WORK| 1 |
SubProject Table
In this example, both subprojects will be associated to the project with id = 1.
Then query the tables with a query similar to this:
select p.id, p.name, sp.id, sp.name
from projects p
inner join subprojects sp on p.id = sp.project_id
where p.id = 1
This is the very basic. You should read about FOREIGN KEYS, and how to define them in your database, so it can help you validate the relationship between records.