MS Access重复记录从相关表中查询

时间:2012-06-14 23:31:04

标签: sql ms-access

我在创建以下报告时遇到问题,正如您可以通过包含的csv看到的那样,查询正在生成重复项。我已经尝试了很多我能找到的SQL语句变体,但没有任何运气。

主要表:tblServers

Server ID,Server name,Hostname,Operating System,Admin Password,Attachments
1,"ESX","ESX",1,"**************","CS6.txt;WUG.docx"
2,"Media","media",2,"**************","Access-Compare-CSV-Tables-Relationship.pl;Updates-WSUS.pdf"
3,"Deploy","deploy",3,"**************","WUG.docx"
5,"Newtest","newtest",2,"************",

辅助表:tblSettings

ID,Server ID,Role,Feature,Application,Setting,sValue
1,1,1,,,1,"C:\inetpub"
2,1,1,,,6,"test.com"
3,1,1,,,4,"testuser"
4,1,1,,,5,"testpassword"
5,2,,7,,4,"root"
6,2,,7,,5,"pword"
7,2,,5,,6,"techtools.hopto.org"

第3表:tblIP

ID,Sebnet,Last Octect,Description
1,"192.168.0",120,"DRAC"
1,"192.168.0",250,"Giga1"
2,"192.168.31",9,"VMWARE"
1,"192.168.31",250,"Giga2"

查询 - 结果

"Server ID","Server name","Hostname","Operating System","Admin Password","Attachments","Role","Feature","Application","Setting","sValue","IP","Description"
1,"ESX","ESX",1,"**************","CS6 License.txt;WUG.docx",1,,,1,"C:\inetpub","192.168.0.250","Giga1"
1,"ESX","ESX",1,"**************","CS6 License.txt;WUG.docx",1,,,6,"test.com","192.168.0.250","Giga1"
1,"ESX","ESX",1,"**************","CS6 License.txt;WUG.docx",1,,,4,"testuser","192.168.0.250","Giga1"
1,"ESX","ESX",1,"**************","CS6 License.txt;WUG.docx",1,,,5,"testpassword","192.168.0.250","Giga1"
1,"ESX","ESX",1,"**************","CS6 License.txt;WUG.docx",1,,,1,"C:\inetpub","192.168.0.120","DRAC"
1,"ESX","ESX",1,"**************","CS6 License.txt;WUG.docx",1,,,6,"test.com","192.168.0.120","DRAC"
1,"ESX","ESX",1,"**************","CS6 License.txt;WUG.docx",1,,,4,"testuser","192.168.0.120","DRAC"
1,"ESX","ESX",1,"**************","CS6 License.txt;WUG.docx",1,,,5,"testpassword","192.168.0.120","DRAC"
1,"ESX","ESX",1,"**************","CS6 License.txt;WUG.docx",1,,,1,"C:\inetpub","192.168.31.250","Giga2"
1,"ESX","ESX",1,"**************","CS6 License.txt;WUG.docx",1,,,6,"test.com","192.168.31.250","Giga2"
1,"ESX","ESX",1,"**************","CS6 License.txt;WUG.docx",1,,,4,"testuser","192.168.31.250","Giga2"
1,"ESX","ESX",1,"**************","CS6 License.txt;WUG.docx",1,,,5,"testpassword","192.168.31.250","Giga2"
2,"Media","media",2,"**************","**;**",,7,,4,"root","192.168.31.9","VMWARE"
2,"Media","media",2,"**************","Access-Compare-CSV-Tables-Relationship.pl;**",,7,,5,"pword","192.168.31.9","VMWARE"
2,"Media","media",2,"**************","Access-Compare-CSV-Tables-Relationship.pl;**",,5,,6,"**","192.168.31.9","VMWARE"

非常感谢任何帮助

编辑:

查询SQL

SELECT
  tblServers.[Server ID],
  tblServers.[Server name],
  tblServers.Hostname,
  tblServers.[Operating System],
  tblServers.[Admin Password],
  tblServers.Attachments,
  tblSettings.Role,
  tblSettings.Feature,
  tblSettings.Application,
  tblSettings.Setting,
  tblSettings.sValue,
  [tblIP]![Subnet] & "." & [tblIP]![Last Octect] AS IP,
  tblIP.Description
FROM (tblServers
  LEFT JOIN tblSettings ON tblServers.[Server ID] = tblSettings.[Server ID])
  INNER JOIN tblIP ON tblServers.[Server ID] = tblIP.[Server ID]
;

1 个答案:

答案 0 :(得分:0)

这是Select Distinct有用的地方。

您只需在查询中添加Distinct关键字,如下所示,即可获得无重复的值。

SELECT Distinct tblServers.[Server ID], tblServers.[Server name], tblServers.Hostname, 
    tblServers.[Operating System], tblServers.[Admin Password], tblServers.Attachments,
    tblSettings.Role, tblSettings.Feature, tblSettings.Application, tblSettings.Setting,
    tblSettings.sValue, [tblIP]![Subnet] & "." & [tblIP]![Last Octect] AS IP,
    tblIP.Description
FROM (tblServers 
  LEFT JOIN tblSettings ON tblServers.[Server ID] = tblSettings.[Server ID]) 
  INNER JOIN tblIP ON tblServers.[Server ID] = tblIP.[Server ID];