简体   繁体   English

使用SQL查询搜索中间字符串

[英]search the inbetween string using sql query

In wp_postmeta table meta_value contains the value http/google.co.in/ wp_postmeta表中, meta_value包含值http/google.co.in/

If the user searches for http/google.co.in/testing , then the resultset should contain http/google.co.in/ 如果用户搜索http/google.co.in/testing ,则结果集应包含http/google.co.in/

I tried with following query: 我尝试了以下查询:

SELECT * FROM wp_postmeta WHERE meta_value LIKE '%http/google.co.in/testing%' 

but it did not return the expected result. 但是它没有返回预期的结果。

How can i get the desired result? 我如何获得理想的结果? How can I use regular expressions to get this result? 如何使用正则表达式获得此结果?

this '%http/google.co.in/testing%' means you are looking for any string that contains 'http/google.co.in/testing' so 'http/google.co.in/' won't return any result because it doesn't contain the string you are looking for. '%http/google.co.in/testing%'表示您正在寻找包含'http / google.co.in / testing'的任何字符串,因此'http/google.co.in/'不会返回任何字符串结果,因为它不包含您要查找的字符串。 You can use SUBSTR() to search for a part your string. 您可以使用SUBSTR()搜索字符串的一部分。

if you use sql: ..... LIKE '%http/google.co.in/testing%' , then DB will look for any string containing "http/google.co.in/testing" . 如果您使用sql:..... LIKE '%http/google.co.in/testing%' ,则DB将查找包含"http/google.co.in/testing"任何字符串。 Note that your desired result does not contain "testing" inside. 请注意,您所需的结果中不包含"testing"

Let's try: 我们试试吧:

SELECT * FROM wp_postmeta WHERE meta_value LIKE CONCAT('%', SUBSTR('http/google.co.in/testing', 1, 18), '%')

As others have pointed out, LIKE searches for strings containing the entire search string (plus any other strings where the %s are), not strings containing a substring of your string. 正如其他人指出的那样,LIKE会搜索包含整个搜索字符串的字符串(加上%s所在的任何其他字符串),而不是包含字符串的子字符串的字符串。 I don't know if there is such a command. 我不知道是否有这样的命令。 You suggested perhaps regex is your answer. 您建议也许正则表达式是您的答案。 MySQL does in fact have a regex command, which a quick Google search would show you the documentation for here: http://dev.mysql.com/doc/refman/5.7/en/regexp.html Do you need further help determining your regex, or is this enough? 实际上,MySQL确实有一个regex命令,通过Google的快速搜索,您会在这里看到文档: http : //dev.mysql.com/doc/refman/5.7/en/regexp.html您是否需要进一步的帮助来确定您的文档?正则表达式,还是足够? If you want further help, you'll have to clarify exactly what regex would be searching for, because you'd have to understand the components of your search string to know what will be constant and what will be variable. 如果您需要进一步的帮助,则必须确切说明正则表达式将要搜索的内容,因为您必须了解搜索字符串的组成部分才能知道什么是常数和什么是变量。

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

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