查询不存在元数据键的行

问题描述:

SELECT DISTINCT wposts.ID AS ID
FROM `wp_posts` AS wposts
JOIN `wp_postmeta` AS postmeta ON (wposts.ID = postmeta.post_id)
WHERE wposts.post_type = 'post'
AND wposts.ID NOT IN (
    SELECT wp_posts.ID FROM wp_posts, `wp_postmeta` AS postmeta2
    WHERE wp_posts.ID = postmeta2.post_id
    AND postmeta2.meta_key = 'z_latitude'
)
ORDER BY wposts.post_date DESC

查询是一团糟.我只是想查询所有没有特定元键的行.上面,我查询所有具有元键的行,然后将它们从外部查询中排除.我不知道怎么写更好.

The query is a mess. I simply want to query all rows that do not have a certain meta key. Above, I query all rows that have the meta key and then exclude them from the outer query. I don't know how to write this better.

通过 left join 使用 outer 连接并抓取没有 加入:

Use an outer join via a left join and grab rows that didn't join:

SELECT DISTINCT wposts.ID AS ID
FROM `doxy_posts` AS wposts
left JOIN `doxy_postmeta` AS postmeta ON wposts.ID = postmeta.post_id
WHERE postmeta.post_id is null
ORDER BY wposts.post_date DESC