2016-03-02 98 views
-1

我有一個分層表,用於根據給定的查詢過濾器從中獲取一組記錄的博客文章的線索評論。 表結構是這樣的從查詢表中獲取所有記錄的單個查詢

ID, PARENTID, APPROVEDSTATUS, COMMENTSTATUS, COMMENTORID, MODERATORID.... 

一個博客第一評論將有parentId的= 0註釋經過趨緩的過程,其中主持人(可以是博客的主人)可以凸顯評論其他版主審覈或者他們可以對此做出迴應。然後,該指導員可以回覆來自博主的任何回覆,然後鏈可以繼續。

我需要在java運行頁面中顯示所有博客的所有評論的列表。該頁面允許用戶根據評論狀態,該博客的星級評定,該評論的評分等來過濾所顯示的記錄。其中一些過濾器適用於主要評論,一些適用於每個子級別。 我最初是如何處理這個問題的,我是第一級評論,並且每次都會遍歷每個孩子和兒童,以便它們以線程格式顯示。 應用過濾器時出現問題。當我在主要級別應用過濾器時,例如commentstatus是「突出顯示」註釋的位置,這將返回0條記錄,因爲此狀態位於子級別。同樣的星級也在父母級別。 有沒有一種方法可以獲取所有記錄並應用過濾條件,然後按照正確的順序。

PRIMARY QUERY: 

    select id, parentid, approvedstatus, 
      commentstatus, personname, 
      actiontime 
    from v_comment_tree where parentCommentid=0; 

QUERY 2: 

select id, parentid, approvedstatus, commentstatus, 
     personname, actiontime 
    from v_comment_tree 
where parentCommentid=40611 
    and commentstatus = 'flag'; 

通過傳遞第一條評論中的id來激發查詢2。

[Java的部分]

public CommentList getBlogComments(){ 
    /* get the filters from the request if any and built where clause. 
    On loading first time there will be no filters*/ 
    filter1 = " and commentstatus = 'flag'"; 
    filter2 = " and rate = 5"; 
    /* execute the query by adding these filters to the original query and iterate over the loop and add the result to the CommentList. 
     This is the parentid. */ 
    /* check if this id has children. For this I call another function passing this id */ 
    getSubBlogComments(id, filter1+filter2); 

    return CommentList; 
} 

private void getSubBlogComments(id, String filters){ 
    /* here I execute Query 2 by applying the same filter 
    and iterate over the loop and add the result to the CommentList. 
     This is the parentid. */ 
    /* check if this id has children. With this I again call this function passing this id */ 

    getSubBlogComments(id, filters); 

} 

時,因爲在第一個查詢應用過濾器的返回我沒有結果,所以我無法從孩子的結果,其中有可能是有標誌狀態的一個記錄的問題出現。如果有一個查詢會以正確的順序獲取父子記錄,並將整個結果應用於過濾器。即使那樣,也存在這個問題。如果我得到子記錄,我需要獲取原始父項並根據該子項記錄顯示整個線程。我應該寫一個程序嗎?

+0

您可以發表您的相關代碼嗎? – Aleksej

+0

你想要java代碼嗎?或SQL查詢? – Rache

+0

我認爲Java代碼會很好,因爲你正在做所有的工作。 –

回答

0

這可以通過一系列匿名視圖完成'allcomments'將從根節點開始獲取線程中的註釋列表。我認爲最好是在sql中完成而不是在java中完成,因爲這很容易實現。

--this will get all the comments in the thread starting at parent = 0 
with allcomments as 
    (select 
    id, parentid, approvedstatus, commentstatus, personname, actiontime 
    from v_comment_tree 
    start with 
    parentCommentid=0 
    connect by prior id = parentCommentid) 
select * from allcomments where <your filter conditions here>