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_to_test2 に (test2_id = 1) AND (test2_id = 2) の行がある test1 テーブルから値を選択する必要があります。したがって、これが必要です:

+----+-------+
| 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 に答える