比较 MySQL 中的两个字符串

发布于 2024-11-14 20:35:04 字数 574 浏览 2 评论 0原文

我当前的查询(不起作用)

SELECT url
FROM table
WHERE
    'http://www.longurl.com/some/string' like '%' . url . '%'

url
longurl.com

我试图将 longurl.com 的“表”中的记录与包含 longurl.com 的任何比较 URL 进行匹配。

应该匹配比较的 URL,例如:

http://www.longurl.com/some/string
http://longurl.com
http://longurl.com/some/string

在这个 PHP 示例中,比较很容易:

$url = "http://www.longurl.com/some/string";    
if(strstr($url, 'longurl.com')) {
    echo "success";
} else {
    echo "failure";
}

My current query (doesn't work)

SELECT url
FROM table
WHERE
    'http://www.longurl.com/some/string' like '%' . url . '%'

table

url
longurl.com

I am trying to match the record in "table" for longurl.com to any compared URL that contains longurl.com.

Should match compared URLs such as:

http://www.longurl.com/some/string
http://longurl.com
http://longurl.com/some/string

In this PHP example, it is very easy to compare:

$url = "http://www.longurl.com/some/string";    
if(strstr($url, 'longurl.com')) {
    echo "success";
} else {
    echo "failure";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

半城柳色半声笛 2024-11-21 20:35:04

好吧,怎么样:(

SELECT url 
  FROM table 
 WHERE INSTR('http://www.longurl.com/some/string', url) > 0

评论中格式不太好,所以我再次添加它作为答案。)

Okay, what about:

SELECT url 
  FROM table 
 WHERE INSTR('http://www.longurl.com/some/string', url) > 0

(Formatting didn't work so well in the comments, so I added it again as an answer.)

放低过去 2024-11-21 20:35:04

如果您需要获取包含 longurl.com 的所有 URL,请尝试使用:

SELECT url
FROM table
WHERE
url like '%longurl.com%'

它应该可以工作。

If what you need is getting all URLs containing longurl.com then try using:

SELECT url
FROM table
WHERE
url like '%longurl.com%'

It should work.

神经大条 2024-11-21 20:35:04

尝试删除或转义 URL 字符串中的某些字符。或者省略 http: 部分..我相信 : 可能会混淆解析器...

so try   %//longurl.com/%

甚至

%longurl.com%  if possible

甚至

http://longurl.com/%

Try removing or escaping some of the characters in your URL string. OR omitting the http: part .. I believe the : could be confusing the parser ...

so try   %//longurl.com/%

or even

%longurl.com%  if possible

or even

http://longurl.com/%
狼性发作 2024-11-21 20:35:04

这?

SELECT url
FROM table
WHERE
    url like '%http://%longurl.com%'

或者您也可以使用%longurl.com%

this?

SELECT url
FROM table
WHERE
    url like '%http://%longurl.com%'

or you can also use %longurl.com%

柒夜笙歌凉 2024-11-21 20:35:04

MySQL 中不存在连接运算符 .,您必须使用 CONCAT() 函数改为:

SELECT url
FROM table
WHERE 'http://www.longurl.com/some/string' LIKE CONCAT('%', url, '%');

The concatenation operator . does not exist in MySQL, you have to use the CONCAT() function instead:

SELECT url
FROM table
WHERE 'http://www.longurl.com/some/string' LIKE CONCAT('%', url, '%');
站稳脚跟 2024-11-21 20:35:04

怎么样:

SELECT url 
FROM table
WHERE url LIKE '%longurl.com%string%'

或者,就像你的 PHP 一样:

SELECT url 
FROM table
WHERE url LIKE '%longurl.com%'

What about this:

SELECT url 
FROM table
WHERE url LIKE '%longurl.com%string%'

or, to be just like your PHP:

SELECT url 
FROM table
WHERE url LIKE '%longurl.com%'
涙—继续流 2024-11-21 20:35:04

您是否在寻找:

LIKE '%longurl.com%'

Are you looking for:

LIKE '%longurl.com%'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文