我有一个表名tblnetworkstatus,我有11列
我在protected_server和affected_service中获取id,我将其存储在我的数据库中,现在我有三种情况
我分别使用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 m.affected_server=s.id
LEFT JOIN tblproducts as p ON m.affected_service=p.id
但是如果我有多个来自affected_server和affected_service的id,就像我将1,2,3,4存储在一个列affected_server或affected_service中那么我该怎么做?
答案 0 :(得分:0)
无需使用union,因为它会提供大量重复数据
您希望通过简单left join
或inner join
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 m.affected_server=s.id
LEFT JOIN tblproducts as p ON m.affected_service=p.id
一列中的多个ID
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(m.affected_server,s.id)
LEFT JOIN tblproducts as p ON m.affected_service=p.id
看看这是否适合您。
答案 1 :(得分:0)
即使您的要求不是很清楚,仍然可以根据您的要求使用以下查询。
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 tblnetworkstatus m
LEFT JOIN tblservers s ON m.affected_server=s.id
LEFT JOIN tblproducts p ON m.affected_service=p.id
WHERE ((m.affected_server='Y' AND m.affected_service='Y') OR m.affected_server='Y' OR m.affected_service='Y');
注意:如果不是您想要的,那么请分享所有3个表样本数据和您想要的输出,以便我可以帮助您。
根据您的最新更新,您可以尝试一下 -
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 tblnetworkstatus m
LEFT JOIN tblservers s ON find_in_set(s.id,m.affected_server)
LEFT JOIN tblproducts p ON find_in_set(p.id,m.affected_service)
WHERE ((m.affected_server='Y' AND m.affected_service='Y') OR m.affected_server='Y' OR m.affected_service='Y');
注意:但这不是一个好主意,因为它不会使用索引并减慢过程。