1

我有一个wp_postswp_icl_translations表,它们在wp_posts.IDwp_icl_translations.trid上相互关联。我想排除所有已经有翻译的帖子。以下查询正在检索我实际想要排除的所有行。

SELECT  p.ID, 
        p.post_title 
 FROM   wp_posts AS p 
            JOIN wp_icl_translations AS  t
             ON p.ID= t.trid 
 WHERE p.post_type = '{$type}' AND 
       p.post_status = 'publish' AND 
       t.language_code='en' AND 
       t.element_type='post_post' 
ORDER BY post_title;
4

3 回答 3

1

使用LEFT JOIN并选择IS NULL

SELECT  p.ID, 
        p.post_title 
 FROM   wp_posts AS p 
 LEFT JOIN wp_icl_translations AS  t ON p.ID= t.trid 
 WHERE  p.post_type = '{$type}' AND 
        p.post_status = 'publish' AND 
        t.trid IS NULL
ORDER BY post_title;
于 2012-08-24T08:55:44.620 回答
0

尝试(未测试)

使用 NOT<>条件

SELECT p.ID,p.post_title 
FROM   wp_posts AS p 
INNER JOIN wp_icl_translations AS  t
             ON p.ID= t.trid 
WHERE p.post_type = '{$type}' AND 
       p.post_status = 'publish' AND 
       t.language_code <>'en' AND 
       t.element_type='post_post' 
ORDER BY post_title;

或者

LEFT JOINIS NULL

SELECT p.ID,p.post_title 
FROM   wp_posts AS p 
LEFT JOIN wp_icl_translations AS  t
             ON p.ID= t.trid 
WHERE p.post_type = '{$type}' AND 
       p.post_status = 'publish' AND 
       t.trid IS NULL
ORDER BY post_title;
于 2012-08-24T08:54:51.957 回答
0

我会说这样的话;使用 (NOT) IN 运算符。但是可能有更好的解决方案(它的性能不应该那么好,也没有经过测试)。

SELECT ID, 
       post_title 
FROM   wp_posts
WHERE  ID NOT IN 
(
      SELECT p.ID
      FROM   wp_posts AS p 
      JOIN   wp_icl_translations AS  t
        ON   p.ID= t.trid 
      WHERE  p.post_type = '{$type}' AND 
             p.post_status = 'publish' AND 
             t.language_code='en' AND 
             t.element_type='post_post' 

)
ORDER BY post_title;  
于 2012-08-24T08:57:51.137 回答