我在 Postgres 中解析 url 时遇到问题。我有一个充满客户和与他们相关联的网址的数据库。我需要一组与每个客户关联的唯一域。我希望能够在查询中进行解析,而不是将结果转储到 Python 并在那里解析。
在 postgres 文档中,我发现了这一点,但不知道如何将其合并到我的查询中:
SELECT alias, description, token FROM ts_debug('http://example.com/stuff/index.html');
alias | description | token
----------+---------------+------------------------------
protocol | Protocol head | http://
url | URL | example.com/stuff/index.html
host | Host | example.com
url_path | URL path | /stuff/index.html
(http://www.postgresql.org/docs/9.3/static/textsearch-parsers.html)
我从一张桌子开始,像这样:
customer_id | url
-------------+--------------------
000001 | www.example.com/fish
000001 | www.example.com/potato
000001 | www.potato.com/artichoke
000002 | www.otherexample.com
到目前为止我的代码:
SELECT customer_id, array_agg(url)
FROM customer_url_table
GROUP BY customer_id
这给了我:
customer_id | unique_domains
-----------------------------
000001 | {www.example.com/fish, www.example.com/potato, www.potato.com/greenery}
000002 | {www.otherexample.com}
我想要这样的表:
customer_id | unique_domains
-----------------------------
000001 | {example.com, potato.com}
000002 | {otherexample.com}
使用 AWS 上的 PostgreSQL 9.3.3 数据库。