如何使用SQL查询从多个表中获取数据

时间:2018-04-19 09:29:05

标签: sql ms-access

我有三个表管理,一对多与电话,传真表:

Administration : Id_Administration, Lib_Administration

Telephone: Id_Phone, Phone_Number, Id_Administration

Fax: Id_Fax, Fax_Number, Id_Administration

管理表包含:

Id_Administration Lib_Administration 
1                 adminstration1                                               
2                 adminstration2

电话表包含:

Id_Phone Phone_Number Id_Administration
1        0313131      1
2        0212121      1
3        0353535      2
4        0343434      2

传真表包含:

Id_Fax  Fax_Number  Id_Administration
1       0323232     1
2       0363636     2
3       0373737     2

我想进行查询以显示此结果:

Id_Administration Lib_Administration Phone_Number Fax_Number
1                 adminstration1     0313131      0323232
                                     0212121      
2                 adminstration2     0353535      0363636
                                     0343434      0373737

我使用了这个查询

SELECT   Administration.Id_Administration, Administration.Lib_Administration,Telephone.Phone_Number, Fax.Fax_Number
FROM     ((Administration INNER JOIN
         Telephone ON Administration.Id_Administration = Telephone.Id_Administration) INNER JOIN
         Fax ON Administration.Id_Administration = Fax.Id_Administration)

但结果是这样迭代的:

Id_Administration Lib_Administration Phone_Number Fax_Number
    1                 adminstration1     0313131      0323232
                                         0212121      0323232
    2                 adminstration2     0353535      0363636
                                         0343434      0363636
                                         0353535      0373737
                                         0343434      0373737

我使用左连接,但我没有得到正确的结果,所以我的查询中的问题在哪里?

2 个答案:

答案 0 :(得分:0)

一切皆有可能,但有时努力实现我们的目标并不值得。您是否正在尝试为表单准备记录集? 在这种情况下,我将有2个子表单,用于电话和传真号码。无需在一个记录集中加载所有内容。

答案 1 :(得分:0)

我不认为原始表结构有效。在发布的DDL中,您具有管理级别的粒度。您想要做的是将电话号码与传真号码配对 - 我将其称为办公级别的粒度。您可以做的是通过创建Office表来添加另一个粒度级别。在插入中,将每个电话和传真号码也链接到此办公室ID。对于“选择”,还将电话和传真表也加入此Id_Office。这应该由组织和办公室配对电话以提供预期的输出。

例如,如果您包含类似于以下的Office表:

CREATE TABLE Administration (Id_Administration int, Lib_Administration varchar(100))
INSERT INTO Administration (Id_Administration, Lib_Administration) VALUES 
 (1, 'adminstration1'), (2, 'adminstration2')

CREATE TABLE Office(Id_Administration int, Id_Office int, Lib_Office varchar(100))
INSERT INTO Office (Id_Administration, Id_Office, Lib_Office) VALUES
 (1, 1, 'A1 Office 1'), (1, 2, 'A1 Office 2')
,(2, 1, 'A2 Office 2'), (2, 2, 'A2 Office 2')

CREATE TABLE Fax (Id_Fax int, Fax_Number char(7), Id_Administration int, Id_Office int)
INSERT INTO Fax (Id_Fax, Fax_Number, Id_Administration, Id_Office) VALUES
 (1, '0323232', 1, 1), (2, '0363636', 2, 1)
,(3, '0373737', 2, 2)

CREATE TABLE Telephone (Id_Phone int, Phone_Number char(7), Id_Administration int, Id_Office int)
INSERT INTO Telephone (Id_Phone, Phone_Number, Id_Administration, Id_Office) VALUES
 (1, '0313131', 1, 1)
,(2, '0212121', 1, 2)
,(3, '0353535', 2, 1)
,(4, '0343434', 2, 2)

SELECT A.Id_Administration, A.Lib_Administration, T.Phone_Number, COALESCE(F.Fax_Number, '') AS Fax_Number
  FROM Administration A LEFT JOIN Office O ON A.Id_Administration = O.Id_Administration
                         LEFT JOIN Fax F ON F.Id_Administration = A.Id_Administration AND F.Id_Office = O.Id_Office
                         LEFT JOIN Telephone T ON T.Id_Administration = A.Id_Administration AND T.Id_Office = O.Id_Office

这会产生输出:

Id_Administration   Lib_Administration  Phone_Number    Fax_Number
                1       adminstration1       0313131       0323232
                1       adminstration1       0212121
                2       adminstration2       0353535       0363636
                2       adminstration2       0343434       0373737

http://sqlfiddle.com/#!18/27964/3/0