简体   繁体   English

选择查询以返回一列包含域名的所有行

[英]Select query to return all rows where a column contains a domain name

I have a column that stores URLs and I would like to generate a select query to pull back all URLs containing particular domain name(s). 我有一列存储URL的列,我想生成一个选择查询以拉回所有包含特定域名的URL。

Example of URLs stored: http://www.fox.com/files/apple.jpg , http://mail.redfox.com/help/index.aspx , http://apple.com/ etc. 存储的URL示例: http : //www.fox.com/files/apple.jpg,http : //mail.redfox.com/help/index.aspx,http : //apple.com/

I know what you're already thinking, just use LIKE. 我知道您已经在想什么,只需使用LIKE。

The reason I can't is: SELECT * WHERE Domains IS LIKE %fox.com% it will return http://www.fox.com/files/apple.jpg along with http://mail.redfox.com/help/index.aspx (since they both contain fox.com). 我不能这样做的原因是: SELECT * WHERE Domains IS LIKE %fox.com%它将在SELECT * WHERE Domains IS LIKE %fox.com%这样的SELECT * WHERE Domains IS LIKE %fox.com%将返回http://www.fox.com/files/apple.jpghttp://mail.redfox.com/help /index.aspx (因为它们都包含fox.com)。

I'm not all the savvy with SQL other than your basic queries, SQL Server can do some sort of regex magic I'm unfamiliar with? 除了您的基本查询之外,我对SQL的了解还不是全部,SQL Server可以执行某种我不熟悉的正则表达式魔术吗?

You could test the domain name for both subdomains and naked domain: 您可以同时为子域和域测试域名:

WHERE Domains IS LIKE '%.fox.com/%' OR Domain IS LIKE '%/fox.com/%'

Sure, you can do it with a REGEX ( [\\/\\.]fox.com\\/ ), but simple SQL might be easier to remember/understand later on. 当然,您可以使用REGEX( [\\/\\.]fox.com\\/ )进行操作,但是以后更容易记住/理解简单的SQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM