Reputation: 143
i have 2 table named projects and tasks
in projects table i have:
id name
---------
1 some
in tasks table i have:
id name project_id
----------------------
1 some 1
Now,how can i select * from task table and get the 'name' from projects table by 'project_id' in table tasks?
thanks
Upvotes: 7
Views: 29094
Reputation: 3111
SELECT t.*, p.[name] FROM tasks t
INNER JOIN projects p
ON t.project_id = p.[id]
WHERE t.project_id = ____
You fill in _ with the project_id you want
Upvotes: 3
Reputation: 382150
select task.id, task.name, proj.id, proj.name
from tasks task left join projects proj on proj.id=task.project_id;
Using left join ensures you get something even if there is no record in the projects table. If you want to ensure coherency, you may do
select task.id, task.name, proj.id, proj.name
from tasks task, projects proj
where proj.id=task.project_id;
Upvotes: 9