问题
I have EXPERIMENTAL_RUNS (runId), each of which have any number of SENSORS (sensorId) associated with them. With that in mind, I have an RS table to join the two:
==========
RS
==========
runId, sensorId
Thus if the run with runId=1 had sensors with sensorId=1, sensorId=6, sensorId=8 in it, there would be 3 entries in the RS table: (runId=1, sensorId=1) (runId=1, sensorId=6) (runId=1, sensorId=8)
Is this really how I would return all EXPERIMENTAL_RUNS that have sensors {11,13,15}? From what I've read, what I seem to want is a nested hash join... Is this what's going to happen?
SELECT a.runId
FROM rs a, rs b, rs c
WHERE
a.runId=b.runId AND
b.runId=c.runId AND
a.sensorId=11 AND
a.sensorId=13 AND
b.sensorId=15
To clarify, I want to return only the EXPERIMENTAL_RUNS that have sensors 11 AND 13 AND 15.
回答1:
Assuming runId, sensorId
are unique in the rs
table, this will find the runId
s that have all 3 sensorId
s:
SELECT runId, COUNT(c) ct
FROM rs
WHERE sensorId IN (11, 13, 15)
GROUP BY runId
HAVING ct = 3
来源:https://stackoverflow.com/questions/13889547/select-all-rows-that-have-at-least-a-list-of-features