简体   繁体   中英

search the inbetween string using sql query

In wp_postmeta table meta_value contains the value http/google.co.in/

If the user searches for http/google.co.in/testing , then the resultset should contain 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. You can use SUBSTR() to search for a part your string.

if you use sql: ..... LIKE '%http/google.co.in/testing%' , then DB will look for any string containing "http/google.co.in/testing" . Note that your desired result does not contain "testing" inside.

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. 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? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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