I already have a working solution to the following problem, but I worry that it is silly or inefficient.
There is a Thing table with columns (id, attributes...) and also a ThingVersion table with columns (id, thing_id, version_attributes...) The problem is to select from Thing where exactly one corresponding ThingVersion row exists.
At the moment I have something like
SELECT Thing.id AS id, Foo.whatever
FROM Thing JOIN Foo ON Thing.id=Foo.thing_id
WHERE Thing.id IN (
SELECT thing_id FROM ThingVersion TV
WHERE 1 = (
SELECT COUNT(*)
FROM ThingVersion TV2
WHERE TV2.thing_id = TV.thing_id)
)
ORDER BY Foo.whatever
It seems to give the correct results, and as an emphatic non-guru it seems self-explanatory, but -- three selects?!?! -- I can't help but feel there must be a better way.
Is there?