I am querying for some posts in the wordpress posts table and grabbing multiple meta values with it by joining the postmeta table multiple times. I also have a number of conditions for both post columns and meta values. The query seems to be working but I am curious if I have put the conditions in the correct place. Comparing the following queries, which will get me the post ids and their associated meta most effectively/efficiently?
This one:
SELECT a.ID as `post_id`, b.meta_value as `metaval1`, c.meta_value as `metaval2`, d.meta_value as `metaval3`
FROM posts AS a
JOIN postmeta AS b ON (b.post_id = a.ID AND b.meta_key = 'metakey1')
LEFT JOIN postmeta as c ON (c.post_id = a.ID AND c.meta_key = 'metakey2')
LEFT JOIN postmeta as d ON (d.post_id = a.ID AND d.meta_key = 'metakey3')
WHERE
b.meta_value != 10
AND a.post_status = 'pending'
AND a.post_date < '2013-03-07 00:00:00'
AND a.post_type = "post"
ORDER BY a.post_date DESC
LIMIT 100;
OR this one:
SELECT a.ID as `post_id`, b.meta_value as `metaval1`, c.meta_value as `metaval2`, d.meta_value as `metaval3`
FROM posts AS a
JOIN postmeta AS b ON (b.post_id = a.ID)
LEFT JOIN postmeta as c ON (c.post_id = a.ID)
LEFT JOIN postmeta as d ON (d.post_id = a.ID)
WHERE
b.meta_key = 'metakey1'
AND b.meta_value != 10
AND c.meta_key = 'metakey2'
AND d.meta_key = 'metakey3'
AND a.post_status = 'pending'
AND a.post_date < '2013-03-07 00:00:00'
AND a.post_type = "post"
ORDER BY a.post_date DESC
LIMIT 100;
Any advice would be appreciated :)