我的 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');
它返回这个
它应该只有一行,因为赔偿表 (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'
要进一步了解有关联接的更多信息,请访问以下链接:
关于SQL Server 查询返回太多记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15389678/