2011-11-26 95 views
3

我是MySQL的新手,我正在嘗試編寫一個相當先進的查詢。但是,嘿!邊幹邊學& Stackoverflow!加入WordPress的高級MySQL查詢

我大概可以在一個不太高級的查詢/查詢中獲取所有數據,並使用PHP對數據進行排序。但我認爲它可以直接在查詢中完成。

以下是我的代碼。如果您不明白,請提問,我會盡力解釋。如果您發現任何錯誤,請幫助我糾正代碼。

該代碼將用於在我的wordpress頁面上顯示不同的字段。不同的fildes將有不同的類別,例如。側邊欄博客,側邊欄頁面,突出顯示博客,突出頁面。它幾乎和普通帖子一樣工作。

這裏是WordPress的數據庫結構:

問題:

我怎麼supose聯接表:wp_posts,wp_term_relationships, wp_term_taxonomy,wp_terms和wp_postmeta?

做高級查詢還是應該使用PHP來處理if/else函數是一種很好的做法嗎?

<?php 

$id = $post->ID; // Gets the ID of current page 

$query = " 
SELECT wp_posts.post_content, wp_posts.ID, wp_terms.slug  # Data from two different tables 

FROM wp_posts 

# Cant figure out how to join the tables 
INNER JOIN wp_postmeta 
ON wp_posts.ID = wp_postmeta.post_id 

INNER JOIN wp_term_relationships 
ON wp_posts.ID = wp_term_relationships.object_id 

INNER JOIN wp_term_taxonomy 
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id 

INNER JOIN wp_terms 
ON wp_term_taxonomy.term_id = wp_terms.term_id 




WHERE 
wp_posts.post_type = 'my-own-post-type'   # Only get specific post-type 
AND 
wp_posts.post_status = 'publish'    # Only get published posts 


AND 

# START - Only get categories specified here 
(
wp_terms.slug = 'category-1' 
OR 
wp_terms.slug = 'category-2' 
OR 
wp_terms.slug = 'category-3' 
OR 
wp_terms.slug = 'category-4' 
OR 
wp_terms.slug = 'category-5' 
) 
# END - Only get categories specified here 


AND 

# I want to be able to include or exclude specific pages, even if category is the right one 
# Exlude = Don't get data if current page ID is found (ID of current page will be checked in meta_value using %,$id,%) 
# Include means: If include value is specifyed, then get data ONLY if ID is found (ID of current page will be checked in meta_value using %,$id,%) 
# If not exclude and include are set, it should get the data 

# START - Include exclude 
(
# exclude is set, so check if page id match anywhere. If it IS found it it should not get the data, otherwise YES 
wp_postmeta.meta_key = 'exclude' AND wp_postmeta.meta_value <> '%,$id,%' 
OR 
# include is set, so check if page id match anywhere. If it IS NOT found it it should not get the data, otherwise YES 
wp_postmeta.meta_key = 'include' AND wp_postmeta.meta_value = '%,$id,%' 
OR 
# If exclude and include aren't set it should get the data 
wp_postmeta.meta_key <> 'exkludera' AND wp_postmeta.meta_key <> 'include' 
) 
# END - Include exclude 


"; 



$result = mysql_query($query); 

while($row = mysql_fetch_array($result)) 
{ 

// collect all fields in category 1 (eg. sidebar) 
if($row['slug'] == 'category-1'){ 
$all_fields_from_category_1 = $all_fields_from_category_1.' 
<div id="field-sidebar"> 
'.$row['post_content'].' 
</div> 
';} 

// collect all fields in category 1 (eg. highlight) 
if($row['slug'] == 'category-2'){ 
$all_fields_from_category_2 = $all_fields_from_category_2.' 
<div id="field-highlight"> 
'.$row['post_content'].' 
</div> 
';} 



} // end while 



// sidebar 
echo '<div id="container-sidebar">'. 
$all_fields_from_category_1. 
'</div>'; 


// highlight 
echo '<div id="container-highlight">'. 
$all_fields_from_category_1. 
'</div>'; 

?> 

新的版本,新的問題:

SELECT DISTINCT wp_posts.post_content, wp_posts.ID, wp_terms.slug 
FROM wp_posts 
JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id 
JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id 
JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id 
JOIN wp_terms ON wp_term_taxonomy.term_id = wp_terms.term_id 
WHERE wp_posts.post_type = 'my-own-fields' 

AND wp_posts.post_status = 'publish' 
AND wp_terms.slug IN 
('field1', 'field2', 'field3', 'field4', 'field5', 'field6') 
AND 
(
wp_postmeta.meta_key = 'exlude' AND wp_postmeta.meta_value <> '$id' 
OR wp_postmeta.meta_key = 'include' AND wp_postmeta.meta_value = '$id' 
OR wp_postmeta.meta_key <> 'exlude' AND wp_postmeta.meta_value <> 'include' #LAST ROW 
) 

的 「#LAST ROW」 給我回的數據,即使排除或包括設置。這是因爲它匹配表wp_postmeta中的另外兩行。表wp_postmeta的

例子:

meta_id  post_id  meta_key meta_value 
1   30   include  18 
2   30   _edit_lock 1322225789:1 
3   30   _edit_last 1 

如果meta_key inklude或排除爲斜面我想返回數據的POST_ID可以找到...

例塞納里奧當我要回數據:

meta_id  post_id  meta_key meta_value 
2   30   _edit_lock 1322225789:1 
3   30   _edit_last 1 

任何想法,我怎麼能解決這個問題? 聲明:如果未找到包含排除或包含當前標識的行。返回數據。

更多的例子:

的POST_ID是30

如果塞納里奧是:

meta_id  post_id  meta_key meta_value 
1   30   include  18 
2   30   _edit_lock 1322225789:1 
3   30   _edit_last 1 

然後,我想回去wp_posts.post_content,wp_posts.ID和wp_terms.slug只要AS $ ID是18。

如果塞納里奧是:

meta_id  post_id  meta_key meta_value 
1   30   exclude  18 
2   30   _edit_lock 1322225789:1 
3   30   _edit_last 1 

然後,我想回去wp_posts.post_content,wp_posts.ID和wp_terms.slug只要$ ID不是18

如果塞納里奧是:

meta_id  post_id  meta_key meta_value 
2   30   _edit_lock 1322225789:1 
3   30   _edit_last 1 

然後我想找回wp_posts.post_content,wp_posts.ID和wp_terms.slug。

+0

你能提供一個帶有排除的Post_id和你希望返回的例子嗎 –

+0

Adam,你明白我想達到什麼目的嗎? – Hakan

+0

我相信我是。當'排除:18'時,當'id = 18'時你不需要'post_id'的任何記錄,對嗎?這比簡單地格式化原始查詢更復雜。我會再仔細考慮一下。 –

回答

6

您的連接已正確完成。在刪除您的評論和額外的行時,您會發現查詢並不是那麼複雜。我做出的唯一真正改變是將您的wp_terms.slug的OR列表更改爲IN聲明。這只是爲了減少代碼重複,清理外觀,而不是改變功能。

SELECT p.post_content, p.ID, t.slug 
FROM wp_posts AS p 
INNER JOIN wp_postmeta AS pm ON p.ID = pm.post_id 
INNER JOIN wp_term_relationships AS tr ON p.ID = tr.object_id 
INNER JOIN wp_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id 
INNER JOIN wp_terms AS t ON tt.term_id = t.term_id 
WHERE p.post_type = 'my-own-post-type' 
    AND p.post_status = 'publish' 
    AND t.slug IN 
     ('category-1', 'category-2', 'category-3', 'category-4', 'category-5') 
    AND 
    (
     (pm.meta_key = 'exclude' AND pm.meta_value <> '$id') 
     OR (pm.meta_key = 'include' AND pm.meta_value = '$id') 
     OR (pm.meta_key <> 'exclude' AND pm.meta_value <> 'include') 
    ) 
+0

謝謝!將嘗試明天,並回到你身邊。祝你有個好的一天。 – Hakan

+0

你是對的!加入工作!強硬我有問題的最後一個:###或pm.meta_key <>'exclude'AND pm.meta_key <>'include'###問題是,有兩個其他的meta_key的設置在帖子上(其他兩行):_edit_last和_edit_lock。如果沒有設置meta_key exclude或include,我想要獲取一次數據。上面的代碼即使設置了包含或排除,也可以返回數據。任何想法如何解決?例如。 「如果meta_key包含或排除無法找到,請獲取數據一次」 – Hakan

+0

@Hakan我很難理解你的意思。你能不能更新你的問題並解釋你在找什麼? –