简体   繁体   English

SQL查询联接2个表,仅显示具有匹配数据的行

[英]SQL query to join 2 tables and only display rows with matching data

UPDATE: 更新:

OK, so the queries in your responses work great! 好的,因此您回复中的查询效果很好! Now, I've come across some less than desirable output. 现在,我遇到了一些不太理想的输出。 It appears in the log table there are sometimes multiple logs under the same evid. 它出现在日志表中,有时在同一个evid下有多个日志。 This causes the query to return multiple rows with the same event info. 这将导致查询返回具有相同事件信息的多行。 Is there a way to possible concatenate all the log.text with matching log.evid that way the output is limited to only the desired information? 是否有可能将所有log.text与匹配的log.evi连接起来,从而将输出限制为仅所需的信息?

I am trying to formulate a mysql query that will select certain columns from a table and compare them with certain columns from a different table on the same DB. 我正在尝试制定一个mysql查询,该查询将从一个表中选择某些列,并将它们与同一DB上不同表中的某些列进行比较。 I want to display only the rows with matching event IDs (evid). 我只想显示具有匹配事件ID(evid)的行。 The two tables are named status and log The only columns I need from the events.log table are named evid and text 这两个表的命名statuslog我从唯一需要的列events.log表被命名为evidtext

The query I came up with displays all active alerts in the Zenoss console: 我提出的查询在Zenoss控制台中显示所有活动警报:

SELECT device, component, summary, count, ownerid, evid
  FROM events.status
 WHERE severity >2;

Basically I need to figure out how to join events.status and events.log and only display rows that have a matching evid 基本上,我需要弄清楚如何连接events.statusevents.log ,只显示具有匹配evid

I only have read access to this DB, but if write access is necessary, I could possibly sweet talk the right folks to gain access. 我仅具有对该数据库的读取访问权限,但是如果需要写入访问权限,我可能可以说合适的人来获得访问权限。

Your help is greatly appreciated, as I am a total DB noob. 非常感谢您的帮助,因为我是DB的新手。

SELECT device, component, summary, count, ownerid, l.evid,l.text  
FROM events.status s inner join events.log l on s.evid=l.evid 
WHERE severity >2;

use inner join 使用内部联接

 select l.text, s.device, s.component, s.summary, s.count, s.ownerid, s.evid 
 from  events.status s inner join events.logs l on s.evid=l.evid 
 where s.severity>2

try 尝试

SELECT S.device, S.component, S.summary, S.count, S.ownerid, L.evid,L.text 
FROM events.status S
join events.log L
on S.evid=L.evid
WHERE S.severity >2
SELECT device, component, summary, count, ownerid, st.evid, lg.text
  FROM events.status st
 INNER JOIN events.log lg ON st.evid = lg.evid
 WHERE st.severity >2
SELECT device, component, summary, count, ownerid, events.status.evid, events.log.* 
FROM events.status, events.log 
WHERE events.status.evid = events.log.evid
AND severity >2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM