-1
SELECT wposts.*, wpostmeta.* 
FROM wp_posts wposts, wp_postmeta wpostmeta, wp_postmeta wpostmeta2, wp_postmeta wpostmeta3 
WHERE 
      wposts.ID = wpostmeta.post_id 
  AND wposts.ID = wpostmeta2.post_id 
  AND wposts.ID = wpostmeta3.post_id 
  AND wpostmeta.meta_key = 'listing_subtype' 
  AND wpostmeta.meta_value = 'Seattle' 
  AND wpostmeta2.meta_key = 'district' 
  AND wpostmeta2.meta_value = 'Breadview' 
  AND wpostmeta3.meta_key = 'price_current' 
  AND wpostmeta3.meta_value BETWEEN 0 AND 800000
  AND wposts.post_status = 'publish' 
  AND wposts.post_type = 'vreb_property' 
ORDER BY wposts.post_date DESC 
LIMIT 0, 20

我现在正在查看这个查询,并认为我应该提高它的效率。而且我一直在尝试很多变化,我认为最好要求 SO 输入。

4

1 回答 1

0

您想使用group byandhaving子句来执行此操作。要获取帖子列表:

select p.id
from wp_post_meta p
group by p.id
having sum(case when p.meta_key = 'listing_subtype' and p.meta_value = 'Seattle' 
                then 1 else 0 end) > 0 and
       sum(case when p.meta_key = 'district' and p.meta_value = 'Breadview'
                then 1 else 0 end) > 0 and
       sum(case when p.meta_key = 'price_current' and cast(p.meta_value as float) BETWEEN 0 AND 800000
                then 1 else 0 end) > 0

注意:这是未经测试的,所以它可能有语法错误。

要获取所有信息,请重新加入帖子:

select p.*
from wp_posts p join
     (select p.id
      from wp_post_meta p
      group by p.id
      having sum(case when p.meta_key = 'listing_subtype' and p.meta_value = 'Seattle' 
                      then 1 else 0 end) > 0 and
             sum(case when p.meta_key = 'district' and p.meta_value = 'Breadview'
                      then 1 else 0 end) > 0 and
             sum(case when p.meta_key = 'price_current' and cast(p.meta_value as float) BETWEEN 0 AND 800000
                      then 1 else 0 end) > 0
    ) p2
    on p.id = p2.id
于 2013-02-26T00:05:56.177 回答