SQL LIKE“%value%”的反相 [英] Inverse of SQL LIKE '%value%'

查看:376
本文介绍了SQL LIKE“%value%”的反相的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含域名的MySQL表:

I have a MySQL table containing domain names:

+----+---------------+
| id | domain        |
+----+---------------+
|  1 | amazon.com    |
|  2 | google.com    |
|  3 | microsoft.com |
|    |     ...       |
+----+---------------+

我希望能够通过此表搜索完整的主机名(即www.google.com)。如果是相反的方式,表中包含完整的URL,我将使用:

I'd like to be able to search through this table for a full hostname (i.e. 'www.google.com'). If it were the other way round where the table contained the full URL I'd use:

SELECT * FROM table WHERE domain LIKE '%google.com%'

但是倒不是那么简单。我目前的想法是搜索完整的主机名,然后逐渐剥离域的每个部分,并再次搜索。 (即搜索www.google.com,然后搜索google.com)

But the inverse is not so straightforward. My current thinking is to search for the full hostname, then progressively strip off each part of the domain, and search again. (i.e. search for 'www.google.com' then 'google.com')

这不是特别有效或聪明,必须有更好的方法。我相信这是一个常见的问题,毫无疑问很容易解决!

This is not particular efficient or clever, there must be a better way. I am sure it is a common problem, and no doubt easy to solve!

推荐答案

类似:

SELECT domain FROM table WHERE 'www.google.com' LIKE CONCAT('%', domain);

SELECT domain FROM table WHERE 'www.google.com' LIKE CONCAT('%', domain, '%');

这不是特别有效,但它工作。

It's not particularly efficient but it works.

这篇关于SQL LIKE“%value%”的反相的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆