2017-06-14 55 views
0

我在Postgres中創建了一個限制結果數量的查詢,但由於某種原因,我的限制子句顯示的是我要求的少一個結果。這裏是查詢:Postgres限制顯示錯誤的行數

select articles.title, articles.slug, count.views 
from articles, 
    (select path, count(path) as views 
     from log 
     where status = '200 OK' 
     and path != '/' 
     group by path 
     order by views desc limit 3 
) count 
WHERE articles.slug LIKE LTRIM(count.path, '/article/'); 

但是當我運行它時,我只得到前2行。如果我將3改爲4,我會得到前3行,依此類推。有什麼建議麼?

+1

在子查詢中選擇的行由where子句過濾掉。 –

+0

如果你不使用通配符,你爲什麼使用'LIKE'?這與使用'articles.slug = LTRIM(count.path,'/ article /');' –

+0

'的內部連接基本相同當我刪除限制時,將顯示所有6個相關行,並且如果使用限制3偏移量1按照要求返回前3行,所以我相信戈登的回答是正確的。 –

回答

0

當我刪除限制,顯示所有6相關的行,如果我使用限制3偏移量1返回前3行所要求的,所以我相信戈登的答案是正確的。 - Gerry de Caires