SQL语句使用Like AND Like调用object_id - Not Or

时间:2014-10-07 11:50:16

标签: mysql sql

一如既往地感谢您的帮助。我已经被困在这个上几天了,无法理解它。最后的沟渠努力在这里张贴。

我想要实现的目标 - 抓住所有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迁移出来了,不幸的是,这是最好的解决方案!

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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%'