Jer*_*ens 1 sql database sql-server sql-server-2008
我的SQL Server数据库中有3个表.
它们链接在一起,如图所示(线条连接到图片中的右侧行)

我有一个查询,它应该返回所有补偿tblreparations信息,其中包含有关修复内容的一些信息,但它会返回3次修复,一次为客户端(荷兰语中的klant)分配给它的每台笔记本电脑,而补偿表(荷兰语的修理)laptopID每行只包含一个
这是查询:
SELECT AankopenReparaties.Id,
AankopenReparaties.KlantenId,
AankopenReparaties.actietype,
AankopenReparaties.voorwerptype,
laptopscomputers.merk,
laptopscomputers.model,
laptopscomputers.info,
AankopenReparaties.info,
AankopenReparaties.Prijs,
AankopenReparaties.lopend
FROM AankopenReparaties, laptopscomputers
WHERE (aankopenreparaties.lopend = 'lopend');
Run Code Online (Sandbox Code Playgroud)
它返回这个

它应该只有一行,因为赔偿表(aankopenreparaties)只包含一行一个 laptopID
有谁知道如何解决这一问题?
请帮助,因为它应尽快修复(这是学校的任务)
您返回太多记录的原因是因为您的查询生成了两个表的笛卡尔积.您需要告诉服务器这两个表是如何相互关联的.
SELECT AankopenReparaties.Id,
AankopenReparaties.KlantenId,
AankopenReparaties.actietype,
AankopenReparaties.voorwerptype,
laptopscomputers.merk,
laptopscomputers.model,
laptopscomputers.info,
AankopenReparaties.info,
AankopenReparaties.Prijs,
AankopenReparaties.lopend
FROM AankopenReparaties
INNER JOIN laptopscomputers
ON AankopenReparaties.LaptopID = laptopscomputers.ID -- specify relationship
WHERE aankopenreparaties.lopend = 'lopend'
Run Code Online (Sandbox Code Playgroud)
要进一步了解联接,请访问以下链接: