Joining/summing WordPress 数据库查询中的自定义字段
Joining/summing custom fields in WordPress database query
尽管用谷歌搜索了 2 小时并尝试了各种 JOINS、UNIONS 和 ALIASES 等等,我仍然无法在一个 table 中检索 wp_postmeta 中两个子查询的结果 table 来自 WordPress 数据库(我什至不关心这里的 wp_posts,我感兴趣的所有数据都在 postmeta 中)。
我在数据库中有自定义字段(使用 ACF 插件管理),如下所示,一个键用于区域,另一个键用于数值。
| ID | meta_key | meta_value |
| 1 | group_name | Japan |
| 1 | facility_index | 0.83 |
| 2 | group_name | USA |
| 2 | facility_index | 4.51 |
我需要一个 returns 以下的查询,以便我可以按区域计算聚合(例如,所有设施的 facility_index 的总和,按 country/region,即按group_name):
| ID | group_name | facility_index |
| 1 | Japan | 0.83 |
| 2 | USA | 4.51 |
此查询两次列出 group_name,而不是在第 3 列中列出 facility_index:
SELECT post_id, meta_value, result2.meta_value FROM
(
(
SELECT post_id, meta_value
FROM wp_postmeta
WHERE meta_key = "group_name"
)
UNION
(
SELECT post_id, meta_value
FROM wp_postmeta
WHERE meta_key = "facility_index"
)
) AS result2
GROUP BY post_id
编辑以显示完全符合我要求的最终查询,感谢下面接受的答案:
SELECT
meta1.meta_value AS group_name,
meta2.meta_value AS facility_index,
SUM(meta2.meta_value) AS 'fac_index_geo'
FROM wp_posts post
LEFT JOIN wp_postmeta meta1 ON meta1.post_id = post.ID AND meta1.meta_key = 'group_name'
LEFT JOIN wp_postmeta meta2 ON meta2.post_id = post.ID AND meta2.meta_key = 'facility_index'
WHERE
post.post_type = 'facility'
GROUP BY group_name
据我了解,您正在寻找自定义 SQL 查询 - 即您不想使用 WP_Query
对象。
为此,我将使用一些 JOINS 而不是子查询。
此外,我鼓励您查询 post_type
,否则您可能会从 autosaves/revisions 或类似的错误结果中获取数据。
/*
INNER JOIN
This query will only return posts that contain all meta values
i.e. all 'post' items with group_name AND facility_index
*/
$sql = "
SELECT
post.ID,
meta1.meta_value AS group_name,
meta2.meta_value AS facility_index
FROM {$wpdb->posts} post
INNER JOIN {$wpdb->postmeta} meta1 ON meta1.post_id = post.ID AND meta1.meta_key = 'group_name'
INNER JOIN {$wpdb->postmeta} meta2 ON meta2.post_id = post.ID AND meta2.meta_key = 'facility_index'
WHERE
post.post_type = 'post'
";
$data = $wpdb->get_results( $sql );
只需确保更新 post_type 条件以匹配您所需的 post 类型。
或者,如果您还想获得缺少元值的 post,您可以使用此查询
/*
LEFT JOIN
This query will return all posts even if they have missing meta values
i.e. if group_name or facility_index is empty, then NULL is returned in the column
*/
$sql = "
SELECT
post.ID,
meta1.meta_value AS group_name,
meta2.meta_value AS facility_index
FROM {$wpdb->posts} post
LEFT JOIN {$wpdb->postmeta} meta1 ON meta1.post_id = post.ID AND meta1.meta_key = 'group_name'
LEFT JOIN {$wpdb->postmeta} meta2 ON meta2.post_id = post.ID AND meta2.meta_key = 'facility_index'
WHERE
post.post_type = 'post'
";
尽管用谷歌搜索了 2 小时并尝试了各种 JOINS、UNIONS 和 ALIASES 等等,我仍然无法在一个 table 中检索 wp_postmeta 中两个子查询的结果 table 来自 WordPress 数据库(我什至不关心这里的 wp_posts,我感兴趣的所有数据都在 postmeta 中)。
我在数据库中有自定义字段(使用 ACF 插件管理),如下所示,一个键用于区域,另一个键用于数值。
| ID | meta_key | meta_value |
| 1 | group_name | Japan |
| 1 | facility_index | 0.83 |
| 2 | group_name | USA |
| 2 | facility_index | 4.51 |
我需要一个 returns 以下的查询,以便我可以按区域计算聚合(例如,所有设施的 facility_index 的总和,按 country/region,即按group_name):
| ID | group_name | facility_index |
| 1 | Japan | 0.83 |
| 2 | USA | 4.51 |
此查询两次列出 group_name,而不是在第 3 列中列出 facility_index:
SELECT post_id, meta_value, result2.meta_value FROM
(
(
SELECT post_id, meta_value
FROM wp_postmeta
WHERE meta_key = "group_name"
)
UNION
(
SELECT post_id, meta_value
FROM wp_postmeta
WHERE meta_key = "facility_index"
)
) AS result2
GROUP BY post_id
编辑以显示完全符合我要求的最终查询,感谢下面接受的答案:
SELECT
meta1.meta_value AS group_name,
meta2.meta_value AS facility_index,
SUM(meta2.meta_value) AS 'fac_index_geo'
FROM wp_posts post
LEFT JOIN wp_postmeta meta1 ON meta1.post_id = post.ID AND meta1.meta_key = 'group_name'
LEFT JOIN wp_postmeta meta2 ON meta2.post_id = post.ID AND meta2.meta_key = 'facility_index'
WHERE
post.post_type = 'facility'
GROUP BY group_name
据我了解,您正在寻找自定义 SQL 查询 - 即您不想使用 WP_Query
对象。
为此,我将使用一些 JOINS 而不是子查询。
此外,我鼓励您查询 post_type
,否则您可能会从 autosaves/revisions 或类似的错误结果中获取数据。
/*
INNER JOIN
This query will only return posts that contain all meta values
i.e. all 'post' items with group_name AND facility_index
*/
$sql = "
SELECT
post.ID,
meta1.meta_value AS group_name,
meta2.meta_value AS facility_index
FROM {$wpdb->posts} post
INNER JOIN {$wpdb->postmeta} meta1 ON meta1.post_id = post.ID AND meta1.meta_key = 'group_name'
INNER JOIN {$wpdb->postmeta} meta2 ON meta2.post_id = post.ID AND meta2.meta_key = 'facility_index'
WHERE
post.post_type = 'post'
";
$data = $wpdb->get_results( $sql );
只需确保更新 post_type 条件以匹配您所需的 post 类型。
或者,如果您还想获得缺少元值的 post,您可以使用此查询
/*
LEFT JOIN
This query will return all posts even if they have missing meta values
i.e. if group_name or facility_index is empty, then NULL is returned in the column
*/
$sql = "
SELECT
post.ID,
meta1.meta_value AS group_name,
meta2.meta_value AS facility_index
FROM {$wpdb->posts} post
LEFT JOIN {$wpdb->postmeta} meta1 ON meta1.post_id = post.ID AND meta1.meta_key = 'group_name'
LEFT JOIN {$wpdb->postmeta} meta2 ON meta2.post_id = post.ID AND meta2.meta_key = 'facility_index'
WHERE
post.post_type = 'post'
";