mysql select id and name from other table and join query

前端 未结 2 1242
误落风尘
误落风尘 2020-12-01 21:58

i have 2 table named projects and tasks

in projects table i have:

id   name
---------
1    some

in tasks table i have:



        
相关标签:
2条回答
  • 2020-12-01 22:49
    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

    0 讨论(0)
  • 2020-12-01 22:52
    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; 
    
    0 讨论(0)
提交回复
热议问题