For a Wordpress project, I'd like to merge multiple queries into one using subqueries . I'm using MySQL 5.5.42. The first case would be querying a list of member_ids
from the table wp_categories
, then using the resulting list to query for matching ids in a post_id
column from the table wp_postmeta
.
What I have is this so far
SELECT post_id
FROM wp_postmeta
WHERE meta_value IN (
SELECT member_ids
FROM wp_categories
WHERE id=140 )
AND meta_key='bizid'
This seems to return empty-handed, though when I replace the subquery with a valid value I get valid results.
EDIT: This answer had the solution (https://stackoverflow.com/a/22375303/1817379) leaving me with the query:
SELECT post_id, meta_key
FROM wp_postmeta, wp_categories
WHERE wp_categories.id = 140
AND FIND_IN_SET(wp_postmeta.meta_value, wp_categories.member_ids)
AND wp_postmeta.meta_key ='bizid'
Also, thanks @sa289 for the clue: it turns out the subquery returns a string rather than a list, something the IN clause won't handle.