gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:34:01 25 4
gpt4 key购买 nike

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

我已经成功编写了一个有效的查询 - 但我认为一定有更好的方法。我的查询完全是 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

最佳答案

我认为你可以这样做:

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

关于mysql - 具有多个 postmeta 左连接的 Wordpress 自定义 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25997629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com