2011-02-01 167 views
0

試圖構建一個有點複雜的WordPress查詢。通過最新帖子WordPress查詢幫助

  1. 把所有的WordPress作者
  2. 有10分以上的帖子發表(我遇到什麼麻煩)
  3. 排序作者只作者:在一個查詢中,我想。

這是我原來的查詢:

SELECT wp_users.ID, display_name, user_url, user_email, MAX(post_date) as date FROM wp_users, wp_posts WHERE wp_users.ID = wp_posts.post_author AND wp_posts.post_status = 'publish' AND wp_posts.post_type = 'post' GROUP BY display_name ORDER BY date DESC; 

該查詢返回所有作者甚至那些9分或更低發表的帖子。

這裏是與統計有關的職位查詢:

SELECT wp_users.ID, display_name, user_url, user_email, MAX(post_date) as date, COUNT(post_date) as post_count FROM wp_users, wp_posts WHERE wp_users.ID = wp_posts.post_author AND wp_posts.post_status = 'publish' AND wp_posts.post_type = 'post' GROUP BY display_name ORDER BY date DESC; 

在這個查詢,你可以看到,我已經添加了:

COUNT(post_date) as post_count 

哪些精美返回的一切。

只有當我加入這個WHERE子句,確實查詢休息

post_count > 9 

我收到此錯誤信息:在

未知列'POST_COUNT 'where子句'

任何想法,爲什麼這是happing?我的理論:

  1. 不會與分組
  2. 工作或MySQL不允許超過一個AS語句

如果你能提供一些線索,我將不勝感激。

謝謝。

回答

1

變化GROUP BY

後查詢post_count > 9

HAVING post_count > 9的WHERE部分的條件最終的查詢將會SELECT wp_users.ID, display_name, user_url, user_email, MAX(post_date) as date, COUNT(post_date) as post_count FROM wp_users, wp_posts WHERE wp_users.ID = wp_posts.post_author AND wp_posts.post_status = 'publish' AND wp_posts.post_type = 'post' GROUP BY display_name HAVING post_count > 9 ORDER BY date DESC;

+0

在哪裏可以將HAVING子句與查詢的其餘部分相關聯? – user598952 2011-02-01 19:35:47

+0

我更改了帖子以顯示完整查詢 – Ass3mbler 2011-02-01 19:36:57

1

您正在尋找的「具有」操作。

SELECT wp_users.ID, display_name, user_url, user_email, MAX(post_date) as date, COUNT(post_date) as post_count FROM wp_users, wp_posts WHERE wp_users.ID = wp_posts.post_author AND wp_posts.post_status = 'publish' AND wp_posts.post_type = 'post' GROUP BY display_name HAVING post_count>9 ORDER BY date DESC;