具有多个 postmeta 左连接的 Wordpress 自定义 SQL 查询

Posted

技术标签:

【中文标题】具有多个 postmeta 左连接的 Wordpress 自定义 SQL 查询【英文标题】:Wordpress custom SQL query with multiple postmeta left joins 【发布时间】:2014-11-17 19:25:16 【问题描述】:

我需要编写一个查询,根据是否包含特定值的各种不同元键返回数据。

我已经成功编写了一个有效的查询 - 但我认为必须有更好的方法。我的查询完全是 LEFT JOINS 和 OR 语句。如果有人可以改进我的查询,我将非常感激!

野兽来了:

SELECT wp.post_type, nice_name, COUNT(wp.post_type)  as counter
FROM wp_posts wp
LEFT JOIN wp_postmeta a ON ( a.post_id = wp.ID AND a.meta_key =  'multiple_publication_issues_0_issue_number' )                             
LEFT JOIN wp_postmeta b ON ( b.post_id = wp.ID AND b.meta_key =  'multiple_publication_issues_1_issue_number' ) 
LEFT JOIN wp_postmeta c ON ( c.post_id = wp.ID AND c.meta_key =  'multiple_publication_issues_2_issue_number' ) 
LEFT JOIN wp_postmeta d ON ( d.post_id = wp.ID AND d.meta_key =  'multiple_publication_issues_3_issue_number' ) 
LEFT JOIN wp_postmeta e ON ( e.post_id = wp.ID AND e.meta_key =  'multiple_publication_issues_4_issue_number' ) 
LEFT JOIN wp_postmeta f ON ( f.post_id = wp.ID AND f.meta_key =  'multiple_publication_issues_5_issue_number' ) 
LEFT JOIN wp_postmeta g ON ( g.post_id = wp.ID AND g.meta_key =  'multiple_publication_issues_6_issue_number' ) 
LEFT JOIN wp_postmeta h ON ( h.post_id = wp.ID AND h.meta_key =  'multiple_publication_issues_7_issue_number' ) 
LEFT JOIN wp_postmeta i ON ( i.post_id = wp.ID AND i.meta_key =  'multiple_publication_issues_8_issue_number' ) 
LEFT JOIN wp_postmeta j ON ( j.post_id = wp.ID AND j.meta_key =  'multiple_publication_issues_9_issue_number' ) 
LEFT JOIN wp_postmeta k ON ( k.post_id = wp.ID AND k.meta_key =  'multiple_publication_issues_10_issue_number' ) 
LEFT JOIN wp_postmeta l ON ( l.post_id = wp.ID AND l.meta_key =  'multiple_publication_issues_11_issue_number' ) 
LEFT JOIN wp_postmeta m ON ( m.post_id = wp.ID AND m.meta_key =  'multiple_publication_issues_12_issue_number' ) 
LEFT JOIN wp_postmeta n ON ( n.post_id = wp.ID AND n.meta_key =  'multiple_publication_issues_13_issue_number' ) 
LEFT JOIN wp_postmeta o ON ( o.post_id = wp.ID AND o.meta_key =  'multiple_publication_issues_14_issue_number' ) 
LEFT JOIN wp_postmeta p ON ( p.post_id = wp.ID AND p.meta_key =  'multiple_publication_issues_15_issue_number' )                                                        
INNER JOIN post_types ptt ON (wp.post_type = ptt.post_type )
WHERE wp.post_status = 'publish'                            
AND(a.meta_value =  119
OR b.meta_value =  '119'
OR c.meta_value =  '119'
OR d.meta_value =  '119'
OR e.meta_value =  '119'
OR f.meta_value =  '119'
OR g.meta_value =  '119'
OR h.meta_value =  '119'
OR i.meta_value =  '119'
OR j.meta_value =  '119'
OR k.meta_value =  '119'
OR l.meta_value =  '119'
OR m.meta_value =  '119'
OR n.meta_value =  '119'
OR o.meta_value =  '119'
OR p.meta_value =  '119')

GROUP BY wp.post_type, nice_name

【问题讨论】:

codereview.stackexchange.com 【参考方案1】:

我认为你可以这样做:

SELECT 
    wp.post_type, 
    nice_name, 
    COUNT(wp.post_type)  as counter
FROM wp_posts wp
INNER JOIN post_types ptt ON (wp.post_type = ptt.post_type )
WHERE EXISTS
(
    SELECT
        NULL
    FROM
        wp_postmeta
    WHERE
        wp_postmeta.meta_key IN
        (
            'multiple_publication_issues_1_issue_number',
            'multiple_publication_issues_2_issue_number',
            'multiple_publication_issues_3_issue_number',
            'multiple_publication_issues_4_issue_number',
            'multiple_publication_issues_5_issue_number',
            'multiple_publication_issues_6_issue_number',
            'multiple_publication_issues_7_issue_number',
            'multiple_publication_issues_8_issue_number',
            'multiple_publication_issues_9_issue_number',
            'multiple_publication_issues_10_issue_number',
            'multiple_publication_issues_11_issue_number',
            'multiple_publication_issues_12_issue_number',
            'multiple_publication_issues_13_issue_number',
            'multiple_publication_issues_14_issue_number',
            'multiple_publication_issues_15_issue_number',
        )
        AND wp_postmeta.meta_value =  '119'
        AND wp.ID=wp_postmeta.post_id
)
AND wp.post_status = 'publish'
GROUP BY 
    wp.post_type, 
    nice_name

【讨论】:

您的解决方案将查询时间减少了 45%...谢谢! :) 没问题。很高兴帮助:)

以上是关于具有多个 postmeta 左连接的 Wordpress 自定义 SQL 查询的主要内容,如果未能解决你的问题,请参考以下文章

显示具有多个需要累积的值的左连接

具有多个左连接的 SQL 重复行

具有多个左连接和计数的错误计算(Laravel)

Linq to Entity 具有多个左外连接

SQL:具有相似表的多个左连接

具有多个左外连接的 SQL Server 查询挂起