2014-10-07 69 views
0

一如既往感谢您提前给予的帮助。我被困在这个几天,无法摆脱它。最后一次在这里发布信息。SQL语句使用Like和Like来调用object_id - 不是或

我想要实现 - 抓住所有的object_id同时匹配“家”和“夜总会”

这涉及到3个表联接以达到

WP_2_TERMS 
-------------------------------------------------------- 
term_id | name | slug | term_group | 
    3   nightclub  nightclub  0 
    41   house  house-3   0 
-------------------------------------------------------- 

WP_2_TAXONOMY 
------------------------------------------------------- 
term_taxonomy_id | term_id | taxonomy | 
     3     3   category 
     42     41   Music 
------------------------------------------------------- 

WP_2_TERM_RELATIONSHIPS 
------------------------------------------------------- 
object_id | term_taxonomy_id | term_order | 
    13     4      0 
    13     42     0 
    65     3      0 
    65     42     0 
    111     3     0 

------------------------------------------------------- 

因此理想情况下,SQL会输出只是object_id 65 ,但目前它返回0行

到目前为止,这是我的。任何人都可以看到我做错了什么,并谨慎解释吗?

SELECT `rel`.`object_id` 
    FROM `wp_2_term_relationships` as `rel` 
    INNER JOIN `wp_2_term_taxonomy` as `tax` ON rel.term_taxonomy_id = tax.term_taxonomy_id 
    LEFT JOIN `wp_2_terms` as `terms` ON tax.term_id = terms.term_id 
    LEFT JOIN `wp_2_terms` as `terms2` ON tax.term_id = terms2.term_id 
    WHERE terms.name LIKE '%house% AND terms2.name LIKE '%nightclub%' 

而且,是的,我知道这可能是与wp_query做,但我是从WordPress的迁移远所以很遗憾的时刻,这是最好的解决办法!

回答

1

您遇到的问题是,即使你加入表wp_2_terms两次,要加入到相同的列(tax.term_id),因此terms.name永远是一样terms2.name,而因为这些会同样他们不能同时为nightclubhouse

这个标准的方法是使用GROUP BY/HAVING

SELECT rel.object_id 
FROM wp_2_term_relationships AS rel 
     INNER JOIN wp_2_term_taxonomy AS tax 
      ON rel.term_taxonomy_id = tax.term_taxonomy_id 
     INNER JOIN wp_2_terms AS terms 
      ON tax.term_id = terms.term_id 
WHERE terms.Name IN ('house', 'nightclub') 
GROUP BY rel.object_id 
HAVING COUNT(DISTINCT terms.Name) > 1; 

如果使用LIKE是强制性的,而不是简单的等于,你可以重写:

SELECT rel.object_id 
FROM wp_2_term_relationships AS rel 
     INNER JOIN wp_2_term_taxonomy AS tax 
      ON rel.term_taxonomy_id = tax.term_taxonomy_id 
     INNER JOIN wp_2_terms AS terms 
      ON tax.term_id = terms.term_id 
WHERE terms.Name LIKE '%house%' 
OR  terms.Name LIKE '%nightclub%' 
GROUP BY rel.object_id 
HAVING COUNT(CASE WHEN terms.Name LIKE '%house%' THEN 1 END) >= 1 
AND  COUNT(CASE WHEN terms.Name LIKE '%nightclub%' THEN 1 END) >= 1; 
+0

THANKYOU !!我感谢您花时间不仅回答我的问题,而且解释方法! +1 – bossbowser 2014-10-07 12:30:33

+0

也@GarethD如果我想这反向运行,因为我有object_ID但想找到terms.Name我将如何重组此? - 提前致谢 – bossbowser 2014-10-07 13:44:07

0

我觉得应该够了。

SELECT `rel`.`object_id` 
FROM 
    `wp_2_term_relationships` as `rel` 
LEFT OUTER JOIN 
    `wp_2_term_taxonomy` as `tax` ON rel.term_taxonomy_id = tax.term_taxonomy_id 
LEFT OUTER JOIN 
    `wp_2_terms` as `terms` ON tax.term_id = terms.term_id 
WHERE 
    terms.name LIKE '%house%' 
AND 
    terms.name LIKE '%nightclub%' 
+0

感谢您的帮助@kmas但仍它返回ZERO行:/ – bossbowser 2014-10-07 11:56:38

+0

可以肯定,wp_2_term_taxonomy和WP_2_TAXONOMY是同一个表...? – kmas 2014-10-07 11:58:55

+0

它们是不同的表格。总的来说有3个表格。 wp_2_terms - wp_2_taxonomy - wp_2_terms_relationships – bossbowser 2014-10-07 12:07:43