4

我遇到了一种情况,我必须在检查和取消检查条款时显示帖子。

已分配条款的帖子。我有术语“区域”和“美食”,现在我必须选择具有区域“XYZ”和美食“ABC”的帖子。

我尝试过的查询:-

    SELECT p.ID, p.post_title 
      FROM wp_posts p 
 LEFT JOIN `wp_term_relationships` t 
        ON p.ID = t.object_id 
 LEFT JOIN `wp_term_taxonomy` tt 
        ON t.term_taxonomy_id = tt.term_taxonomy_id 
     WHERE tt.term_id IN (".$area.") 
           OR tt.term_id IN (".$cuis.") 
  GROUP BY t.object_id 
    HAVING COUNT( t.term_taxonomy_id ) = 2 
     LIMIT 0,7 

wp_term_taxonomy 的结构如下所示:-

问题是单个表和单个列,并在值之间应用 AND 运算符。

wp_term_relationship

object_id  | wp_term_taxonomy_id | term_order
==============================================
   134     |       36            |    0
______________________________________________
   135     |       36            |    0

wp_posts

    ID     |    post_title       |
==================================
    1      |       Hello world!  |  
__________________________________
    2      |       Test          | 

wp_term_taxnomy

  term_taxonomy_id  term_id     taxonomy    description     parent  count
        =============================================================================
          1                1            category     ------           0        2
4

2 回答 2

1

假设我们有 3 个表:

| test1 |     | test1_to_test2 |         | test2 |
|-------+     +----------------|         +-------|
| id    |-----|  test1_id      |    +----|  id   |
              |  test2_id      |----+

正是你所拥有的结构。

内容:

    test1
+----+-------+     
| id | value |
+----+-------+
|  1 | val1  |
|  2 | val2  |
+----+-------+

    test1_to_test2
|----------+----------|
| test1_id | test2_id |
|----------+----------|
|        1 |        1 |
|        1 |        2 |
|        2 |        1 |
|----------+----------|

 test2
|----+
| id |
|----+
|  1 |
|  2 |
|----+

我们需要从 test1 表中选择值,这些值在 test1_to_test2 中具有 (test2_id = 1) AND (test2_id = 2) 的行。所以,我们想要这个:

+----+-------+
| id | value |
+----+-------+
|  1 | val1  |
+----+-------+

为此,我们将任务分为 2 个子任务:

1.从test1_to_test2test1_id中选择,其中包含两行:

SELECT
    test1_id
FROM
    test1_to_test2
WHERE
    test1_to_test2.test2_id IN (1,2)
GROUP BY
    test1_id
HAVING
    COUNT(test1_id) = 2

2.使用子查询和 IN 运算符从 test1 中选择适当的行(这是我们需要的 SQL):

SELECT
    test1.id,
    test1.`value`
FROM
    test1
WHERE
    test1.id IN
(
SELECT
    test1_id
FROM
    test1_to_test2
WHERE
    test1_to_test2.test2_id IN (1,2)
GROUP BY
    test1_id
HAVING
    COUNT(test1_id) = 2
)

我们得到了我们需要的东西:

+----+-------+
| id | value |
+----+-------+
|  1 | val1  |
+----+-------+

对您的桌子使用相同的方法,您将获得区域为“XYZ”和美食“ABC”的帖子。

于 2013-05-18T11:34:38.940 回答
0

最好的办法是进行两次连接,每个学期一次:

    SELECT p.ID, p.post_title 
      FROM wp_posts p 
 LEFT JOIN `wp_term_relationships` t 
        ON p.ID = t.object_id 
 LEFT JOIN `wp_term_taxonomy` tarea
        ON t.term_taxonomy_id = tt.term_taxonomy_id and tt.term_id IN (".$area.") 
 LEFT JOIN `wp_term_taxonomy` tcuis
        ON t.term_taxonomy_id = tt.term_taxonomy_id and tt.term_id IN (".$cuis.") 
     WHERE tarea.description = 'XYZ' and 
           tcuis.descriptoin = 'ABC'
  GROUP BY t.object_id 
     LIMIT 0,7 ;

此外,您可以将left joins 更改为inner joins - 该where子句无论如何都会这样做。s对于left join“或”条件而不是“和”条件很有用。

于 2013-05-18T16:31:52.677 回答