【发布时间】:2014-07-23 22:15:04
【问题描述】:
我在 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 数据库。
【问题讨论】:
-
为什么在使用 9.3 版本时使用 8.3 版本的手册? 8.3 和 9.3 之间发生了很多变化
-
@frank-heikens,感谢您指出这一点。本文档中的相关部分在 9.3 和 8.3 中是相同的,所以我刚刚更新了链接以反映这一点。
标签: sql postgresql url-parsing