Mysql 使用和从term_Taxonomy表中选择

Mysql 使用和从term_Taxonomy表中选择,mysql,sql,wordpress,Mysql,Sql,Wordpress,我遇到了一种情况,我必须在检查和取消检查的条件下显示帖子 有指定条款的职位。我有“区域”和“美食”这两个词,现在我必须选择一个有“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

我遇到了一种情况,我必须在检查和取消检查的条件下显示帖子

有指定条款的职位。我有“区域”和“美食”这两个词,现在我必须选择一个有“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_术语_分类法的结构如下所示:-

问题在于单表和单列,并在值之间应用and运算符

wp_术语_关系

object_id  | wp_term_taxonomy_id | term_order
==============================================
   134     |       36            |    0
______________________________________________
   135     |       36            |    0
wp_员额

    ID     |    post_title       |
==================================
    1      |       Hello world!  |  
__________________________________
    2      |       Test          | 
wp_术语_税收

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

假设我们有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到test2中有行,test2\u id=1和test2\u id=2。因此,我们希望:

+----+-------+
| id | value |
+----+-------+
|  1 | val1  |
+----+-------+
为此,我们将任务分为两个子任务:

1.从test1_到test2中选择test1_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  |
+----+-------+
我们得到了我们需要的:

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”的帖子。

您最好的选择是进行两次连接,每学期一次:

    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 ;

此外,还可以将左连接更改为内部连接,where子句也会这样做。左连接对于or条件而不是and条件很有用。

您能给出在select语句中使用了以下列的示例记录吗?我已经编辑了这个问题。请检查。用什么来代替“XYZ”和“ABC”?因为描述字段为空。@YaarMallangJeha。您的查询显示有“XYZ”区域和“ABC”区域。因此,使用具有这些值的任何字段。我只是猜测描述,因为它不明显。