MySQL Forums
Forum List  »  Newbie

Re: Query for finding keys (IDs) that only have one of two values associated with it
Posted by: Rick James
Date: April 18, 2014 09:44PM

Here are two solutions. They may differ, based on information about the dataset that you have not shared.

Those with only one:

SELECT post_id, COUNT(*)
FROM tbl
GROUP BY post_id
HAVING COUNT(*) = 1;

Those with _VenueVenue but not _VenueLat:

SELECT a.post_id
FROM tbl AS a
LEFT JOIN tbl AS b ON a.post_id = b.post_id
WHERE a.meta_key = '_VenueVenue'
AND b.meta_key = '_VenueLat'
AND b.post_id IS NULL

That is, do a "self JOIN", with `a` representing VenueVenue and b representing VenueLat, but not finding a VenueLat (IS NULL).

With the first solution, you get only post_id. If you want more than just post_id, use that as a subquery.

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.