获取 sql 语句来引用 2 个以上的表?

Jac*_*ack 3 sql-server-2008

我有以下表格:

员工姓名

============================================================================
|  Name    |   Income_Group   |    Educational_Level  |   Country          |
============================================================================
|  Jack    |   5              |    5                  |   1                |
|  Jill    |   3              |    3                  |   4                |
|  Jane    |   1              |    4                  |   6                |
|  June    |   4              |    2                  |   7                |
============================================================================
Run Code Online (Sandbox Code Playgroud)

国家

==================================================
| ID   |  Country   |  Country_Description       |
==================================================
| 1    | America    |                            |
| 7    | Japan      |                            |
==================================================
Run Code Online (Sandbox Code Playgroud)

收入组

=======================================================
| ID   |  Income_Range        |  Description          |
=======================================================
| 1    | Below US$2500        |                       |
| 5    | US$9000 to US$12000  |                       |
=======================================================
Run Code Online (Sandbox Code Playgroud)

受教育程度

============================================================
| ID   |  Educational_Level   |  Country_Description       |
============================================================
| 1    | PhD                  |                            |
| 7    | Master               |                            |
============================================================
Run Code Online (Sandbox Code Playgroud)

我的目的是将所有值从其他表获取到主表并显示如下:

预期结果

=======================================================================================
|  Name    |   Income_Group              |    Educational_Level  |   Country          |
=======================================================================================
|  John    |   US$9000 to US$12000       |    PhD                |   America          |
|  KoKo    |   US$5000 to US$7000        |    Master             |   Japan            |
|  Kane    |   US$1000 to US$2000        |    B.Degree           |   Thailand         |
|  Ali     |   US$8200 to US$9200        |    College            |   Malaysia         |
=======================================================================================
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下 sql:

select 
    s.name, s.Income_Group, s.Educational_Level, s.Country
from 
    StaffName s, Country c. IncomeGroup ig, EducationalLevel el 
where 
    s.Income_Group = c.ID 
    AND s.Educational_Level = ig.id 
    AND s.Country = el.id
Run Code Online (Sandbox Code Playgroud)

但它没有返回任何结果。

所有表都没有外键或主键。

我可能错过了什么?

RoK*_*oKa 5

我似乎您的某些语法在问题中并不完全正确,但它可能只是一个错字。您似乎也在尝试使用连接表的旧方法。

我还注意到,您必须从StaffName与其他表不匹配(空值?)的地方返回记录。要仍然加入并仅显示没有匹配项的空白,您需要使用外部联接。为简单起见,我LEFT在下面的示例中仅使用了连接。

另外,请注意,我已经在您的选择列表中返回了适当的字段,引用了连接的表,以便获得如您的要求所示的结果

select s.name, ig.Income_Range, el.Educational_Level, c.Country  
from StaffName s 
    left join Country c on s.Income_Group = c.ID
    left join IncomeGroup ig on s.Educational_Level = ig.id
    left join EducationalLevel el on s.Country = el.id
Run Code Online (Sandbox Code Playgroud)

为了更好地理解左连接,这里是 BOL 的摘录。

使用左外连接

考虑在 ProductID 列上连接 Product 表和 ProductReview 表。结果仅显示已撰写评论的产品。要包括所有产品,无论是否为某个产品撰写了评论,请使用 ISO 左外连接。以下是查询:

USE AdventureWorks2008R2; 
GO 
SELECT p.Name, pr.ProductReviewID 
FROM Production.Product p 
    LEFT OUTER JOIN Production.ProductReview pr ON p.ProductID = pr.ProductID 
Run Code Online (Sandbox Code Playgroud)

LEFT OUTER JOIN 在结果中包括 Product 表中的所有行,无论 ProductReview 表中的 ProductID 列是否匹配。请注意,在产品没有匹配的产品评论 ID 的结果中,该行在 ProductReviewID 列中包含一个空值。

我怀疑您关于主键和外键的声明纯粹是为了说我们不必担心它可能对您的连接产生的影响。您可能有自己的理由,但请记住,拥有正确的主键对任何数据库都至关重要。