2013-12-20 88 views
-2

我有新聞表,新聞中有4個類別,我想獲得每個類別的最新消息(按日期排序)。我用了以下查詢:爲什麼順序不通過組合?

SELECT node.nid AS nid, 
    term_data.tid AS term_data_tid, 
    node.type AS node_type, 
    node.vid AS node_vid, 
    node.title AS node_title, 
    node_data_field_blog_overview.field_blog_overview_value AS node_data_field_blog_overview_field_blog_overview_value, 
    node_data_field_blog_overview.field_blog_overview_format AS node_data_field_blog_overview_field_blog_overview_format, 
    term_data.name AS term_data_name, 
    term_data.vid AS term_data_vid, 
    users.name AS users_name, 
    users.uid AS users_uid, 
    node.created AS node_created, 
    node_data_field_blog_overview.field_feed_author_name_value AS node_data_field_blog_overview_field_feed_author_name_value, 
    node.sticky AS node_sticky 
FROM node node 
LEFT JOIN term_node term_node ON node.vid = term_node.vid 
LEFT JOIN term_data term_data ON term_node.tid = term_data.tid 
LEFT JOIN content_field_image node_data_field_image ON node.vid = node_data_field_image.vid 
LEFT JOIN content_type_blog node_data_field_blog_overview ON node.vid = node_data_field_blog_overview.vid 
INNER JOIN users users ON node.uid = users.uid 
WHERE (node.type in ('blog')) AND (node.status <> 0) 
GROUP BY term_data_name order by node_created 
    LIMIT 5 

爲什麼我沒有得到每個類別的最新消息使用上述查詢?

+0

你想說什麼嗎? – user1336827

+0

我越來越舊的消息 – Akki

+0

我得到與desc和asc – Akki

回答

0

我認爲你的問題始於JOIN類型及其順序。我會建議你隨時分組數據,按字段'表使用組作爲FROM表。

爲了簡化,開始剛開每個類別的最新文章的最後時間:

SELECT term_data.name, MAX(node.node_created) AS mostrecent 
FROM term_data 
JOIN term_node 
    ON term_node.tid = term_data.tid 
JOIN node node 
ON node.vid = term_node.vid 
WHERE (node.type in ('blog')) AND (node.status <> 0) 
GROUP BY term_data.name 

然後,你可以用它來獲得更多的文章

SELECT {your field list} 
FROM [insert above query] AS latest_news 
JOIN term_data 
    ON term_data.name = latest_news.name 
JOIN term_node 
    ON term_node.tid = term_data.tid 
JOIN node node 
    ON node.vid = term_node.vid 
    AND latest_news.mostrecent = node.node_created 
JOIN users users 
    ON node.uid = users.uid 
LEFT JOIN content_field_image node_data_field_image 
    ON node.vid = node_data_field_image.vid 
LEFT JOIN content_type_blog node_data_field_blog_overview 
    ON node.vid = node_data_field_blog_overview.vid 
相關問題