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

4.7k Views Asked by At

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. 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?

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. I want to display only the rows with matching event IDs (evid). The two tables are named status and log The only columns I need from the events.log table are named evid and text

The query I came up with displays all active alerts in the Zenoss console:

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

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.

5

There are 5 best solutions below

0
On
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;
1
On

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
1
On

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
0
On
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
0
On
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;