2016-08-23 45 views
0

我正在使用Wordpress创建一个过滤器,并且正在努力弄清楚这个原始查询。MySQL查询:获得一个表的ID号,其中表1的ID在另一个表中的值为X

这是我现在有:

SELECT COUNT(*) FROM active_listings 
    WHERE listing_type = "sales" 
    AND price > 50000 
    AND beds >= 3 
    AND baths >= 4 
    AND active = "1" 
    AND (dstatus IS NULL OR (dstatus != "Temporarily No Showings" AND dstatus != "Contingent")) 
    AND location in (11) 

但我也需要检查wp_postmeta表,看是否有匹配post_id一排有wpcf-activemeta_key1meta_value

+2

https://en.wikipedia.org/wiki/Join_(SQL) –

+0

什么错误? – RGriffiths

+0

等一下 - 你正在使用一个名为'active_listings'的独立表,但是你也在使用** wp_postmeta表?你最近怎么样? –

回答

2

只需使用INNER JOIN.这将从两个表中选择具有相同post_id的记录。您可能需要调整的列名,等等,但它看起来大致是这样的:

SELECT COUNT(*) 
FROM active_listings INNER JOIN wp_postmeta 
ON active_listings.post_id = wp_postmeta.post_id 
WHERE listing_type = "sales" 
AND price > 50000 
AND beds >= 3 
AND baths >= 4 
AND active = "1" 
AND (dstatus IS NULL OR (dstatus != "Temporarily No Showings" AND dstatus != "Contingent")) 
AND location in (11) 
AND meta_key = 'wpcf-active' 
AND meta_value = 1 
相关问题