Finding entries in one MySQL table based on conditions on another table
-
03-07-2019 - |
Question
I've got a table of hardware and a table of incidents. Each hardware has a unique tag, and the incidents are tied to the tag.
How can I select all the hardware which has at least one incident listed as unresolved?
I can't just do a join, because then if one piece of hardware had multiple unresolved issues, it would show up multiple times.
Solution
select distinct(hardware_name)
from hardware,incidents
where hardware.id = incidents.hardware_id and incidents.resolved=0;
OTHER TIPS
Something like this should do it:
Select A.HardwareID A.HadwareName, B.UnresolvedCount
From (Hardware A)
Inner Join
(
Select HardwareID, Count(1) As UnresolvedCount
From Incidents
Where Resolved = 0
Group By HardwareID
) As B On A.HardwareID = B.HardwareID
This can also work
SELECT hd.name, inc.issue, FROM hardware hd INNER JOIN inc ON hd.tag = inc.tag AND inc.issue = 'unresolved' group by hd.name
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow