我正在運行WordPress,我想寫一個自定義查詢,顯示基於一個特定類別的帖子的月度存檔。我使用調試模式來了解如何根據所有帖子獲取每月歸檔,並選擇特定類別。下面是查詢:需要幫助MySQL,加入兩個查詢
獲取montlhy檔案:
SELECT YEAR(post_date) AS `year`, MONTH (post_date) AS `month`,
count(ID) as posts FROM wp_posts
WHERE post_type = 'post' AND post_status = 'publish'
GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC
選擇類別:
SELECT t.*, tt.* FROM wp_terms AS t INNER JOIN wp_term_taxonomy
AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy
IN ('category') AND t.slug = 'blog' ORDER BY t.name ASC
我無法弄清楚如何加入這兩個;)任何幫助表示讚賞!
謝謝。
更新:
還有涉及稱爲term_relationships另一個表,這樣就使得總共4桌。這裏的WordPress的數據庫結構的圖像:http://codex.wordpress.org/images/8/83/WP_27_dbsERD.png
不管怎麼說,這裏是我在哪裏:
SELECT t.*, tt.*, YEAR(post_date) AS `year`, MONTH(post_date) AS `month`,
count(ID) as posts FROM wp_posts, wp_term_relationships AS tr,
wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON t.term_id = tt.term_id
WHERE tt.taxonomy IN ('category') AND t.slug = 'blog'
AND post_type = 'post' AND post_status = 'publish'
AND ID = tr.object_id AND tr.term_taxonomy_id = t.term_id
GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC
我知道這不是各地最好的方式,這就是爲什麼我需要你的幫助;)
謝謝。
對於那些沒有運行WordPress的,可您發佈的'顯示的結果爲每個創建table'涉及的表格?特別是,這兩張表有一個可用於將它們相互關聯的字段? (或者它們可以通過第三個表相互關聯) – 2010-02-09 12:03:24
沒有人不能從這些查詢中找到wp_posts和wp_terms之間的關係,您最好在這裏給我們一些表格方案 – vava 2010-02-09 12:04:23
vava,使用http:// codex .wordpress.org/File:WP_27_dbsERD.png full size http://codex.wordpress.org/images/8/83/WP_27_dbsERD.png 146 kb – osgx 2010-02-09 12:14:50