显示重复记录

时间:2015-06-26 06:33:44

标签: php mysql

我有一个表名tblnetworkstatus,我有11列

Id
issue_name
affected_server
affected_service
issue_type
priority
duration
status
start_date
end_date
description

我在protected_server和affected_service中获取id,我将其存储在我的数据库中,现在我有三种情况

Either both affected_server and affected_service has been selected
Only affected_server is selected
Only affected_service is selected

我分别使用tblnetworkstatus,tblserver和tblproduct上的连接分别从tblserver和tblproducts获取affected_server和affected_service的名称我已经从这里获得了以下查询

SELECT 
    m.issue_name ,m.issue_type ,
    m.priority ,m.status,m.description ,
    m.start_date,m.end_date,m.duration,
    s.name  as server_name,p.name as product_name
    from mod_networkstatus as m 
    LEFT JOIN  tblservers as  s  ON  FIND_IN_SET(s.id,m.affected_server)
    LEFT JOIN tblproducts as  p  ON  FIND_IN_SET(p.id,m.affected_service)

但是如果我有多个来自affected_server和affected_service的id,就像我在单个列affected_server或affected_service中存储1,2,3,4而我得到记录重复而只有受影响的服务器或affetced_service与我不同不希望我想在检索存储1,2,3,4之后在单个coulmn字段中显示这些,那我该怎么办呢?

tblproducts结构

Column        Type      Null    
id            int(10)   No       
type          text      No       
gid           int(10)   No       
name          text      No       
description   text      No  

tblserver结构

Column       Type      Null 
id           int(10)   No        
name         text      No        
ipaddress    text      No        
assignedips  text      No        
hostname     text      No   

表mod_network_status

Column               Type           Null    Default     
id                   int(11)        No       
issue_name           varchar(15)    No       
affected_server      varchar(150)   No       
affected_service     varchar(150)   Yes     NULL     
issue_type           varchar(20)    Yes     NULL     
priority             varchar(20)    Yes     NULL     
duration             varchar(20)    Yes     NULL     
status               varchar(20)    Yes     NULL     
start_date           datetime       Yes     NULL     
end_date             datetime       Yes     NULL     
description          varchar(200)   Yes     NULL     


issue_name       issue_type     server_name     product_name    
service_failure  maintenance    server1         hosting account
service_failure  maintenance    osrstrust       hosting account
test             failure        vpn             HOME USER

现在看看上面的结果,它给了我完美的记录,看看row2,其中一切都是相同的,而不是服务器名称,我想在row1上就像那个server1,osrstrust

1 个答案:

答案 0 :(得分:0)

我建议你做三件事:

  1. 您需要重新构建表 mod_network_status

    Column               Type           Null    Default     
    id                   int(11)        No       
    issue_name           varchar(15)    No       
    server_id            varchar(150)   No       
    service_id           varchar(150)   Yes     NULL     
    issue_type           varchar(20)    Yes     NULL     
    priority             varchar(20)    Yes     NULL     
    duration             varchar(20)    Yes     NULL     
    status               varchar(20)    Yes     NULL     
    start_date           datetime       Yes     NULL     
    end_date             datetime       Yes     NULL     
    description          varchar(200)   Yes     NULL    
    
  2. 您需要了解如何使用左连接参考:https://dev.mysql.com/doc/refman/5.0/en/left-join-optimization.html

  3. FIND_IN_SET 是字符串函数,但您使用的是id(int)。语法FIND_IN_SET(搜索字符串,字符串列表)