-5

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

  • Such downvote, I cri. – LethalUniwhale Aug 26 '15 at 16:12
  • See this URL to get join multiple table [URL][1] [1]: http://stackoverflow.com/questions/11040587/simple-sql-select-from-2-tables-what-is-a-join – Jakir Hossain Aug 26 '15 at 16:16
  • 1
    Do a quick search for a Primer on SQL & read up details like Primary Keys & Foreign Keys & JOINS. Then try a few things that you have learnt & show us your efforts - then you are likely to get more help than downvotes. – PaulF Aug 26 '15 at 16:17

1 Answers1

0

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.

Marcovecchio
  • 1,322
  • 9
  • 19