SQL Wordpress查询 - 内部和外部联接

时间:2013-07-03 19:59:19

标签: mysql wordpress outer-join

我正在尝试获取wp_postmeta表格(meta_key='propdetails' AND meta_value LIKE '%Vacant Unrented Ready%')(meta_key='featured' AND meta_value='on')所有帖子。

我现有的查询(如下)工作正常,除非meta_key'feature'行缺少。然后,该帖子不会被退回。

同样,为了匹配行,我还需要检索我想要meta_value meta_key='martygeocoderlatlng'。但是,如果缺少该meta_key,则根本不会返回整个帖子。

所以我需要使用现有的查询并使meta_key='featured'meta_key='martygeocoderlatlng'可选,所以如果它们不存在,我仍然可以进行其他比较并获取所需的数据。

(我认为这可以通过某种方式使用OUTER JOIN来完成,但我无法完全理解语法。)

这是我现有的查询,它只返回w__postmeta表中存在meta_key ='featured'和meta_key ='martygeocoderlatlng'的行(当然,满足WHERE子句中的其他条件):

SELECT 
    p.*,
    pm.*,
    pm_featured.meta_value AS featured,
    pm_latlng.meta_value AS latlng

FROM 
    wp_posts p
    JOIN wp_postmeta pm
      ON pm.post_id = p.ID
    JOIN wp_postmeta pm_propdetails
      ON (pm_propdetails.post_id = p.ID AND pm_propdetails.meta_key = 'propdetails')
    JOIN wp_postmeta pm_featured
      ON (pm_featured.post_id = p.ID AND pm_featured.meta_key = 'featured-property')
    JOIN wp_postmeta pm_latlng
      ON (pm_latlng.post_id = p.ID AND pm_latlng.meta_key = 'martygeocoderlatlng')

    JOIN wp_term_relationships tr
      ON tr.object_id = p.ID
    JOIN wp_term_relationships tr_taxrel
      ON (tr_taxrel.object_id = p.ID AND tr_taxrel.term_taxonomy_id = 12)

WHERE
    (pm_propdetails.meta_value LIKE '%Vacant Unrented Ready%'
        OR
     pm_featured.meta_value = 'on')

    AND p.post_type = 'property'
    AND p.post_status = 'publish'

GROUP BY p.ID
ORDER BY p.post_date DESC

1 个答案:

答案 0 :(得分:1)

这是一个非常激烈的查询。如果不更好地了解你的表格结构,很难确切知道如何有效地做到这一点(似乎可以合并其中一些JOINS)。

只需编辑原始查询即可获得以下信息:

SELECT 
    p.*,
    pm.*,
    pm_featured.meta_value AS featured,
    pm_latlng.meta_value AS latlng

FROM 
    wp_posts p
    JOIN wp_postmeta pm
      ON pm.post_id = p.ID
    JOIN wp_postmeta pm_propdetails
      ON (pm_propdetails.post_id = p.ID AND pm_propdetails.meta_key = 'propdetails')
    LEFT JOIN wp_postmeta pm_featured
      ON (pm_featured.post_id = p.ID AND pm_featured.meta_key = 'featured-property')
    LEFT JOIN wp_postmeta pm_latlng
      ON (pm_latlng.post_id = p.ID AND pm_latlng.meta_key = 'martygeocoderlatlng')

    JOIN wp_term_relationships tr
      ON tr.object_id = p.ID
    JOIN wp_term_relationships tr_taxrel
      ON (tr_taxrel.object_id = p.ID AND tr_taxrel.term_taxonomy_id = 12)

WHERE
    (pm_propdetails.meta_value LIKE '%Vacant Unrented Ready%'
        OR
     (pm_featured.meta_value = 'on' OR pm_feature.meta_value IS NULL))

    AND p.post_type = 'property'
    AND p.post_status = 'publish'

GROUP BY p.ID
ORDER BY p.post_date DESC

它只是使用LEFT JOIN来尝试返回meta_key可能丢失的帖子,然后修改了WHERE部分以试图考虑pm_featured.meta_value是否为NULL。

如果您提供更多信息,可以尝试其他版本。

编辑: 根据你的问题,这可能也有效。在黑暗中真的只是一个巨大的镜头=)

SELECT T2.*, T1.featured, T1.latlng FROM
(
    SELECT post_id, SUM(IF((meta_key='propdetails' AND meta_value LIKE '%Vacant Unrented Ready%') OR ((meta_key='featured' AND meta_value='on')),1,0)) AS Keeper
    , MAX(IF(meta_key = 'featured-property',meta_value,NULL)) AS featured
    , MAX(IF(meta_key = 'martygeocoderlatlng',meta_value,NULL)) AS latlng
    FROM wp_postmeta 
    GROUP BY post_id
    HAVING Keeper>0
) AS T1
JOIN
wp_posts AS T2
ON T1.post_id=T2.ID
AND T2.post_type = 'property'
AND T2.post_status = 'publish'
JOIN
wp_term_relationships AS T3
ON T2.ID=T3.object_id AND T3.term_taxonomy_id = 12
GROUP BY T2.ID
ORDER BY T2.post_date DESC

由于第二个查询最终起作用(有点令人惊讶,我做了很多假设;)这个应该稍快一点,更容易理解...... (注意:特别是如果meta_key表中有(meta_valuewp_postmeta)的综合索引。)

SELECT T2.*, T1.featured, T1.latlng FROM
(
    SELECT postmetas_info.post_id
    , MAX(IF(meta_key = 'featured-property',meta_value,NULL)) AS featured
    , MAX(IF(meta_key = 'martygeocoderlatlng',meta_value,NULL)) AS latlng
    FROM 
    (
        SELECT DISTINCT post_id FROM wp_postmeta WHERE (meta_key='propdetails' AND meta_value LIKE '%Vacant Unrented Ready%') OR (meta_key='featured' AND meta_value='on')
    ) AS postmetas_applicable
    JOIN
    wp_postmeta AS postmetas_info
    ON postmetas_applicable.post_id=postmetas_info.post_id
    GROUP BY postmetas_info.post_id
) AS T1
JOIN
wp_posts AS T2
ON T1.post_id=T2.ID
AND T2.post_type = 'property'
AND T2.post_status = 'publish'
JOIN
wp_term_relationships AS T3
ON T2.ID=T3.object_id AND T3.term_taxonomy_id = 12
GROUP BY T2.ID
ORDER BY T2.post_date DESC

上一个查询中的SUM(IF())很有趣,但可能没有那么高效,因为它会评估表中的每一行。上面的查询首先按照post_id的方式进行,然后只根据符合条件的post_id获取相应的数据。