Row selection based on subtable data in MySQL

Posted by Felthragar on Stack Overflow See other posts from Stack Overflow or by Felthragar
Published on 2012-06-03T16:23:35Z Indexed on 2012/06/03 16:40 UTC
Read the original article Hit count: 280

Filed under:

I've been struggling with this selection for a while now, I've been searching around StackOverflow and tried a bunch of stuff but nothing that helps me with my particular issue. Maybe I'm just missing something obvious.

I have two tables: Measurements, MeasurementFlags

"Measurements" contain measurements from a device, and each measurement can have properties/attributes attached to them (commonly known as "flags") to signify that the measurement in question is special in some way or another (for instance, one flag may signify a test or calibration measurement). Note: One record per flag!

Right, so a record from the "Measurements" table can theoreticly have an unlimited amount of MeasurementFlags attached to it, or it can have none.

Now, I need to select records from "Measurements", that have an attached "MeasurementFlag" valued "X", but it must also NOT have a flag valued "Y" attached to it.

We're talking about a fairly large database with hundreds of millions of rows, which is why I'm trying to keep all of this logic within one query. Splitting it up would create too many queries, however if it's not possible to do in one query I guess I don't have a choise.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about mysql