-1

今天早上让这段代码工作,去吃午饭,现在它没有显示正确的结果。它显示了应该显示的正确数量的行,但所有结果都是第一行的重复。

SELECT 
    client_main.serial, 
    client_main.make, 
    client_main.model, 
    client_deploy.ticket,  
    client_deploy.techID, 
    client_deploy.installDate, 
    client_deploy.updateDate, 
    client_main.status, 
    client_main.type, 
    client_software.operating, 
    client_software.operating_license, 
    client_profile.buildID, 
    client_profile.department, 
    client_main.warrentyStart, 
    client_main.warrentyEnd, 
    client_hardware.cpu, 
    client_hardware.memory, 
    client_hardware.diskSpace1, 
    client_hardware.diskSpace2, 
    client_hardware.diskSpace3, 
    client_software.antivirus, 
    client_software.antivirus_version, 
    client_software.office, 
    client_software.office_license
FROM 
    client_main, 
    client_deploy, 
    client_hardware, 
    client_network, 
    client_profile, 
    client_software
WHERE client_main.id = client_deploy.id
    AND client_deploy.id = client_hardware.id
    AND client_hardware.id = client_profile.id
    AND client_profile.id = client_software.id

更新:

根据指出的错误修复代码。现在查询只显示一个结果。

SELECT client_main.serial, client_main.make, client_main.model, client_deploy.ticket, client_main.status, client_software.operating,  client_profile.username, client_hardware.cpu, client_hardware.diskSpace3, client_software.antivirus, client_network.ip

FROM client_main 
inner join client_deploy on client_deploy.id = client_main.id
inner join client_hardware on client_hardware.id = client_main.id
inner join client_network on client_network.id = client_main.id
inner join client_profile on client_profile.id = client_main.id
inner join client_software on client_software.id = client_main.id

样本结果:

serial  make    model   ticket  status  operating   username    cpu diskSpace3  antivirus   ip
123 Delld   Lattitude1  654897  2   4   dhenning1   13  13  4   2

解决方案查询:

SELECT client_main.serial, client_deploy.ticket, client_software.operating, client_profile.username, client_hardware.cpu, client_network.ip
FROM client_main
INNER JOIN client_deploy ON client_deploy.id = client_main.id
INNER JOIN client_hardware ON client_hardware.id = client_main.id
INNER JOIN client_profile ON client_profile.id = client_main.id
INNER JOIN client_network ON client_network.id = client_main.id
INNER JOIN client_software ON client_software.id = client_main.id
4

2 回答 2

1

如果您对 JOIN 使用 SQL92 语法,您的查询会更清楚:

SELECT m.serial, m.make, m.model, d.ticket, d.techID, d.installDate, d.updateDate, 
  m.status, m.type, s.operating, s.operating_license, p.username, p.buildID, 
  p.department, m.warrentyStart, m.warrentyEnd, h.cpu, h.memory, h.diskSpace1, 
  h.diskSpace2, h.diskSpace3, s.antivirus, s.antivirus_version, s.office, 
  s.office_license
FROM client_main AS m
INNER JOIN client_deploy AS d ON m.id = d.id
INNER JOIN client_hardware AS h ON d.id = h.id
INNER JOIN client_network AS n ON ...wait woops?!...
INNER JOIN client_profile AS p ON h.id = p.id
INNER JOIN client_software AS s ON p.id = s.id

您的查询中没有加入client_network. 因此,您基本上是在生成笛卡尔积,将行数乘以client_network其他联接表产生的行数。

至于为什么在你去吃午饭前没有问题,如果午饭client_network前只有1行,那么笛卡尔积就不明显了。我猜您或其他人client_network在午餐期间或之后添加了更多行。

PS:我同意@GolezTrol 的评论,似乎不太可能id所有这些表中加入正确的列。

于 2013-07-11T18:59:29.830 回答
0

在不知道您的架构的情况下:

FROM client_main, client_deploy, client_hardware,  
      client_network, client_profile, client_software

您正在从六个不同的表中进行选择,但只加入了四个

WHERE client_main.id = client_deploy.id
    AND client_deploy.id = client_hardware.id
    AND client_hardware.id = client_profile.id
    AND client_profile.id = client_software.id

您缺少一些连接,这就是您得到重复结果的原因。

于 2013-07-11T18:53:19.310 回答