7

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

4

2 回答 2

9
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; 
于 2012-11-30T20:45:42.197 回答
3
SELECT t.*, p.[name] FROM tasks t
INNER JOIN projects p
ON t.project_id = p.[id]
WHERE t.project_id = ____

你用你想要的project_id填写_

于 2012-11-30T20:47:40.857 回答