I have a mysql table which stores maintenance logs for sensors. I'd like to design a query that finds instances where a given sensor was repaired/maintained for the same reason. (Recurring problem finder.)
My table (simplified) looks like this:
id name mask
== ==== ====
11 alpha 0011
12 alpha 0010
13 alpha 0100
14 beta 0001
The mask field is a bitmask where each position represents a particular type of repair. I was able to successfully figure out how to compare the bitmask (per this question) but trying to incorporate it into a query is proving more difficult than I thought.
Given the above sample records, only id's 11 and 12 apply, since they both have a 1
in the third mask position.
Here's what I've tried and why it didn't work:
1. Never finishes...
This query seems to run forever, I don't think it is working the way I want.
SELECT t1.id, t1.name
FROM data t1
LEFT OUTER JOIN data t2
ON (CONV(t1.mask,2,10) & CONV(t2.mask,2,10) > 0)
GROUP BY t1.name
HAVING COUNT(*) >1;
2. Incomplete query...
I was thinking of creating a view, to only parse the sensors that actually have more than one entry in the table. I wasn't sure where to go from here.
SELECT COUNT(t1.name) AS times, t1.name, t1.id, t1.mask
FROM data AS t1
GROUP BY t1.name ASC
HAVING times > 1;
Any suggestions on this?