2017-06-20 20 views
0

如何按路徑模式對包含URL的行進行分組?例如。我們必須有地址:按URL路徑模式分組的MySQL組

1 http://example.com 
2 http://example.com/products 
3 http://example.com/products/some-product 
4 http://example.com/categories 
5 http://example.com/categories/cat1 
6 http://example.com/categories/cat2 
7 http://example.com/categories/cat3 
8 http://example.com/tags 
9 http://example.com/tags/tag1 
10 http://example.com/tags/tag2 
11 http://example.com/tags/tag3 
12 http://example.com/about 

那麼結果將是:

1 http://example.com 
2 http://example.com/products 
3 http://example.com/products/some-product 
4 http://example.com/categories 
5 http://example.com/categories/cat1 
8 http://example.com/tags 
9 http://example.com/tags/tag1 
12 http://example.com/about 

我們知道域名http://example.com。我們需要所有不同的路徑類型。基本上我們想知道不同的網頁有哪些網頁。因此,它是一種http://example.com/ */*/* ...

+0

您需要描述分組背後的邏輯。 1)你如何確定哪些值應該組合在一起2)從相似的哪一個保持。 – Shadow

+0

1)在底部添加評論。 2)我們拿第一個。 – Vygandas

+0

在rdbms中沒有第一個這樣的東西。你需要先由什麼決定。 – Shadow

回答

0

試試這個

Rextester Sample

select * from tbl1 t1 
where exists 
(select 1 
from tbl1 t2 
    group by substring_index(concat(url,'@'),'/',4) 
having t1.id=min(t2.id) 
); 

MYSQL,對於未選擇那些不是在group by列中沒有硬性規定。所以你也可以這樣做。

select * 
from tbl1 
group by 
    substring_index(concat(url,'@'),'/',4) 
order by id 
; 

substring_index(concat(url,'@'),'/',4)

角色將首先在URL的末尾添加一個額外的性格,說@。然後它將截取url直到4th/。最後,如果不添加@,則http://example.com/categories/cat1http://example.com/categories將放入您不想要的相同group

+0

謝謝!這樣可行。也許你可以告訴如何放棄/忽略所有內容?或#在網址? :) – Vygandas

+0

更改了在幾乎所有RDBMS中都可以工作的查詢,要放下(包括)特定字符後面的所有內容,使用'substring_index(url,'?',1)'。閱讀一下'substring_index',這個非常有用的函數。 – Utsav