2013-05-18 20 views
4

我已經陷入了一種情況,在那裏我必須顯示帖子檢查和取消條款。從term_taxnomy表中選擇使用AND

已分配條款的帖子。我有'區'和'美食'的條款,現在我必須選擇區域'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的結構如下所示: -

的問題是單個表和單列,並將與運算值之間。

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 
+0

你可以給你在'select'語句中使用以下列的示例記錄嗎? –

+0

我編輯了這個問題。請檢查。 –

回答

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)和(test2_id = 2)。所以,我們希望這樣的:

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

要做到這一點,我們拆分任務分成2子任務:

1.Select test1_id從test1_to_test2,擁有目前兩行:

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運算符(它是我們需要的SQL)從test1中選擇適當的行:

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

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 join s到inner join秒 - 其中where子句則無妨。 left join將對「或」條件而非「條件」有用。

+0

什麼可以代替'XYZ'和'ABC'?由於說明字段爲空。 –

+0

@YaarMallangJeha。 。 。你的查詢說「有地區'XYZ'和美食'ABC'」。所以使用任何有這些價值的領域。我只是猜測說明,因爲它不明顯。 –