2011-06-26 11 views
0
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 

クエリが混乱します。私は単に特定のメタキーを持たないすべての行を照会したいだけです。 上記では、メタキーを持つすべての行を照会して、それらを外部照会から除外します。 これをより良く書く方法はわかりません。メタデータキーが存在しないクエリ行

+0

他の人の答えを悪用停止してください、あなたはあなたが望むならばコメントを追加できますが、答えは編集しません。 –

答えて

1

はないが参加しましたleft 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 
+0

あなたは 'postmeta.meta_key = 'z_latitude''条件をON句に含めるのを忘れていました。 –

0
SELECT wposts.ID AS ID 
FROM doxy_posts AS wposts 
    LEFT JOIN doxy_postmeta AS postmeta 
    ON wposts.ID = postmeta.post_id 
    AND postmeta.meta_key = 'z_latitude' 
WHERE wposts.post_type = 'post' 
    AND postmeta.post_id IS NULL 
ORDER BY wposts.post_date DESC 

か:

SELECT wposts.ID AS ID 
FROM doxy_posts AS wposts 
WHERE wposts.post_type = 'post' 
    AND NOT EXISTS 
     (SELECT * 
      FROM doxy_postmeta AS postmeta 
      WHERE wposts.ID = postmeta.post_id 
      AND postmeta.meta_key = 'z_latitude' 
     ) 
ORDER BY wposts.post_date DESC