SQL代码显示重复的结果

时间:2013-07-11 18:46:56

标签: mysql sql

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

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

2 个答案:

答案 0 :(得分: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。所以你基本上生成Cartesian product,将client_network中的行数乘以其他连接表的行数。

至于为什么在你去吃午餐之前没有问题,如果午餐前client_network只有一排,那么笛卡尔产品就不会显而易见了。我猜你或其他人在午餐时间或午餐后添加了更多行client_network

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

答案 1 :(得分: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

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