我是MySQL的新手,我正在嘗試編寫一個相當先進的查詢。但是,嘿!邊幹邊學& Stackoverflow!加入WordPress的高級MySQL查詢
我大概可以在一個不太高級的查詢/查詢中獲取所有數據,並使用PHP對數據進行排序。但我認爲它可以直接在查詢中完成。
以下是我的代碼。如果您不明白,請提問,我會盡力解釋。如果您發現任何錯誤,請幫助我糾正代碼。
該代碼將用於在我的wordpress頁面上顯示不同的字段。不同的fildes將有不同的類別,例如。側邊欄博客,側邊欄頁面,突出顯示博客,突出頁面。它幾乎和普通帖子一樣工作。
這裏是WordPress的數據庫結構:
- http://codex.wordpress.org/images/9/9e/WP3.0-ERD.png
- http://codex.wordpress.org/Database_Description
問題:
我怎麼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。
你能提供一個帶有排除的Post_id和你希望返回的例子嗎 –
Adam,你明白我想達到什麼目的嗎? – Hakan
我相信我是。當'排除:18'時,當'id = 18'時你不需要'post_id'的任何記錄,對嗎?這比簡單地格式化原始查詢更復雜。我會再仔細考慮一下。 –