基本上,我需要从 URL 或整个网站名称中获取包含域名和子域名的行,不包括www
.
我的数据库表如下所示:
+----------+------------------------+
| id | website |
+----------+------------------------+
| 1 | https://www.google.com |
+----------+------------------------+
| 2 | http://www.google.co.in|
+----------+------------------------+
| 3 | www.google.com |
+----------+------------------------+
| 4 | www.google.co.in |
+----------+------------------------+
| 5 | google.com |
+----------+------------------------+
| 6 | google.co.in |
+----------+------------------------+
| 7 | http://google.co.in |
+----------+------------------------+
预期输出:
google.com
google.co.in
google.com
google.co.in
google.com
google.co.in
google.co.in
我的 Postgres 查询如下所示:
select id, substring(website from '.*://([^/]*)') as website_domain from contacts
但上面的查询给出了空白网站。那么,我怎样才能得到想要的输出呢?
您必须使用“非捕获”匹配?:
应对非“http://”网站。
like
select
id,
substring(website from '(?:.*://)?(?:www\.)?([^/?]*)') as website_domain
from contacts;
SQL 小提琴:http://sqlfiddle.com/#!17/f890c/2/0 http://sqlfiddle.com/#!17/f890c/2/0
PostgreSQL的正则表达式:https://www.postgresql.org/docs/9.3/functions-matching.html#POSIX-ATOMS-TABLE https://www.postgresql.org/docs/9.3/functions-matching.html#POSIX-ATOMS-TABLE
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)