2015-05-01 73 views
0

想知道在PG 9.3中是否有任何技巧可以幫助我?Postgres Select子組

TableID, CommentID, Comment, Orderid 
1, 1, "Jenn, Looks Good", 1 
2, 1, "Benn, The Doc looks good.", 2 
3, 2, "Like it", 1 
4, 3, "Splling", 1 
5, 3, "Spelling", 2 
6, 3, "Check the Spelling", 3 

我有這張表是評論,而不是更新評論編輯作爲評論ID的下一個「orderid」放置。我在尋找一個選擇將返回此:

Benn, The Doc looks good. 
Like it 
Check the Spelling 

我在尋找的東西通過每個commentID更好的循環,如果可能的。

我目前的解決方案是通過CommentID然後選擇評論其中commentid =?通過orderid desc limit 1命令進入temp,然後選擇臨時表。

我一直在尋找這個https://stackoverflow.com/a/12653000/3562373因爲我有了對評論更多信息父鍵並想出了:

Select comment 
from commentInfo 
inner join (Select * From comment order by orderid desc limit 1) on CommentInfo.CommentID = Comment.CommentID 
where commentinf = (The Group of comments I'm interested in) 

但由於限制適用於整個表它不幫助我

+0

投票的人應該被迫離開原因。我很想知道如何提出一個propor問題。 –

回答

1

如果你想要做的是在派生表中返回的最新版本爲每CommentID(也就是具有最高的OrderID),那麼你可以找到最新的版本,並與加盟:

select t.* from table t 
join (
    select CommentID, max(orderid) orderid 
    from table group by CommentID 
) b on b.CommentID = t.CommentID and b.orderid = t.Orderid