1

I have a wordpress database with a number of custom fields/metadata.

---------
wp_postmeta
---------
post_id
meta_key
meta_value

I want to find all the posts with a meta_key of 'Start Date' and a value of 'NOW()', but not the posts that have the meta_key 'Ongoing' and value of 'Yes'.

I'm a bit confused of how to join the queries:

SELECT * FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_postmeta.meta_key = 'Start Date'
AND wp_postmeta.meta_value > NOW()

with

SELECT * FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_postmeta.meta_key = 'Ongoing'
AND wp_postmeta.meta_value != 'Yes'

And potentially other queries... I think what is throwing me off is that I can't add another WHERE clause to the statement? I think I just need a prod in the right direction because I am missing some basic mysql understanding.

Thank you so much in advance!

1
  • Perhaps I'm misunderstanding your question but when meta_key is 'State Date' obviously it can't be 'Ongoing' at the same time, so there will be no rows with meta_key = 'Ongoing' in your results to filter out.
    – nobody
    Aug 16, 2011 at 11:43

3 Answers 3

1

Try this:

SELECT * FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
AND ((wp_postmeta.meta_key = 'Start Date'
AND wp_postmeta.meta_value > NOW()) OR (wp_postmeta.meta_key = 'Ongoing'
AND wp_postmeta.meta_value != 'Yes'))
2
  • Thank you! This seemed to work but I had issues when trying to apply a sort order to the 'Start Date' meta_key. Where could one apply a ORDER BY wp_postmeta.meta_key? Is there any reason this would be a better solution than the NOT IN solution below?
    – waffl
    Aug 16, 2011 at 13:10
  • I think that's it's cleaner and, in my opinion, might be faster as the database grows. For the Order by just add: ORDER BY wp_postmeta.meta_key at the very end of the query.
    – jschorr
    Aug 16, 2011 at 23:24
0

Try to use

SELECT * FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_postmeta.meta_key = 'Start Date'
AND wp_postmeta.meta_value > NOW() and wp_posts.ID NOT IN (select post_id from wp_postmeta
where wp_postmeta.meta_key = 'Ongoing' AND wp_postmeta.meta_value = 'Yes')
0
0

I remember being there. You don't want to know the unscalable, convoluted solution I came up with.

You're just looking for a couple of paren's and an AND with a NOT.

SELECT * FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
 -- parens make sure everything is grouped correctly and you don't have "and
 -- meta_key - Ongoing and meta_value > NOW()
(wp_postmeta.meta_key = 'Start Date'
 AND wp_postmeta.meta_value > NOW())
-- these conditions are all mutually exclusive, so they could never 
-- simultaneously exist.
AND NOT
(wp_postmeta.meta_key = 'Ongoing'
 AND wp_postmeta.meta_value != 'Yes')

IF you wanted to combine the results, you have the option of using OR. Just replace the AND NOT with an OR and see what happens.

You can also use UNION (though that is almost never done when OR is an option (I do seem to remember that there can be subtle performance differences, but worrying about that is often premature optimization)):

SELECT * FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
wp_postmeta.meta_key = 'Start Date'
AND wp_postmeta.meta_value > NOW()
-- Distinct vs. ALL is not really relevant here because you can't have overlap
UNION DISTINCT
SELECT * FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
wp_postmeta.meta_key = 'Ongoing'
AND wp_postmeta.meta_value != 'Yes'
1
  • Thanks for the prompt response, unfortunately I kept getting syntax errors with the (AND ...). You are right about needing to use parentheses though, can't believe I overlooked something so obvious!
    – waffl
    Aug 16, 2011 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.