如何从3个表中获取值?

时间:2015-05-08 11:34:37

标签: sql-server

CREATE PROCEDURE spJoin3Tables
AS 
BEGIN
    SELECT 
        tbl_Jobs.JobTitle, tbl_Company.CompName
    FROM 
        tbl_Jobs
    INNER JOIN 
        tbl_Company ON tbl_Jobs.CompID = tbl_Company.ID

    SELECT 
        tbl_Cities.CityName
    FROM 
        tbl_Cities
    INNER JOIN 
        tbl_JobCities ON tbl_Cities.ID = tbl_JobCities.CityID
    INNER JOIN 
        tbl_Jobs ON tbl_JobCities.JobID = tbl_Jobs.ID
END

结果是两个表。我想在一个表中获取所有三列 - 查询是什么?

2 个答案:

答案 0 :(得分:0)

您只需将公司表和第一个查询中的列添加到第二个查询中,并确保加入公司ID。

SELECT 
    tbl_Cities.CityName, tbl_Jobs.JobTitle, tbl_Company.CompName
FROM 
    tbl_Cities
INNER JOIN 
    tbl_JobCities ON tbl_Cities.ID = tbl_JobCities.CityID
INNER JOIN 
    tbl_Jobs ON tbl_JobCities.JobID = tbl_Jobs.ID
INNER JOIN 
    tbl_Company ON tbl_Jobs.CompID = tbl_Company.ID

答案 1 :(得分:0)

使用内部联接你可以获得所有日期。如果在ID列中的任何表格中,则表示用户左键加入时为空值

SELECT tbl_Jobs.JobTitle, tbl_Company.CompName , tbl_Cities.CityName
FROM tbl_Jobs
INNER JOIN tbl_Company ON tbl_Jobs.CompID = tbl_Company.ID
INNER JOIN tbl_JobCities ON tbl_JobCities.JobID = tbl_Jobs.ID
INNER JOIN tbl_Cities ON tbl_Cities.ID = tbl_JobCities.CityID