我有一个大型数据库,其中包含“机构”分类法和元字段“价格”的帖子。我需要计算免费机构的数量,即价格为“ 0”。本地WP请求在这里太慢了(30秒),所以我发明了自定义请求:
SELECT COUNT(*) as kount
FROM wp_posts as p
LEFT JOIN wp_postmeta as pm
ON p.ID = pm.post_id
LEFT JOIN wp_term_relationships as wtr
ON p.ID = wtr.object_id
WHERE p.post_type = 'institution'
AND p.post_status = 'publish'
AND pm.meta_key = 'price'
AND pm.meta_value = '0'
结果92456超过了帖子总数
select count(*) from wp_posts
返回86317。好的,我认为某些帖子具有多个价格字段,但没有:
select post_id, count(*) kount from wp_postmeta
where meta_key='price'
group by post_id
having kount>1
返回空集。我的SQL怎么了?