如何从3个表中获取值?

时间:2021-10-17 15:36:29
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

The result is two tables. I want to get all three columns in one table - what will be the query?

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

2 个解决方案

#1


You just need to add the company table and the columns from the first query to the second query and make sure to join on the company id.

您只需将公司表和第一个查询中的列添加到第二个查询,并确保加入公司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

#2


USING INNER JOIN U CAN GET ALL DATE. IF IN CASE ANY TABLE IN ID COLUMNS NULL VALUE THEN USER LEFT JOIN

使用内部联接你可以获得所有日期。如果在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

#1


You just need to add the company table and the columns from the first query to the second query and make sure to join on the company id.

您只需将公司表和第一个查询中的列添加到第二个查询,并确保加入公司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

#2


USING INNER JOIN U CAN GET ALL DATE. IF IN CASE ANY TABLE IN ID COLUMNS NULL VALUE THEN USER LEFT JOIN

使用内部联接你可以获得所有日期。如果在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