mysql> SELECT * FROM nodes
-> WHERE hostName IN ('localhost', 'm1iafw') OR
-> address IN ('localhost', 'm1iafw');
+----------------------+-----------+-----------+
| id | hostName | address |
+----------------------+-----------+-----------+
| 9131891219333790492 | NULL | m1iafw |
| 15289714606300179720 | localhost | NULL |
| 15886756565768587967 | m1iafw | 10.7.7.12 |
| 18400354826544934228 | m1iafw | NULL |
+----------------------+-----------+-----------+
Above is a simplified version of a query I'm working on. I would like to write this with one IN
operator so I don't have to repeat list of host names multiple times. There could be hundreds of host names in the list, potentially.
What I want is something like:
SELECT * FROM nodes
WHERE (hostName, address) INTERSECTS ('localhost', 'm1iafw');
I don't need the list of matching values like INTERSECT would provide. I just need a boolean result like with IN. Do the sets intersect or not.
Also, MySQL doesn't support the INTERSECT operator. Nor WITH. The usual advice is to rewrite the INTERSECT in a different form using joins, but I don't know how to do that since I'm not intersecting two tables.
Furthermore, what I really want to do is a bit more complex, even. Given the ID numbers of the nodes from the query up top, I want to search another table for matches, where any one of three columns might contain one of the matching IDs. With my hypothetical INTERSECTS operator I could write the query as:
SELECT * FROM alerts
WHERE (analyzerNodeId, sourceNodeId, targetNodeId) INTERSECTS
(SELECT id FROM nodes
WHERE (hostName, address) INTERSECTS ('localhost', 'm1iafw'));
Any suggestions on how I can do this? My SQL-fu is not so strong.