user6617474
user6617474

Reputation: 113

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?

Upvotes: 0

Views: 73

Answers (3)

ArthuruhtrA
ArthuruhtrA

Reputation: 27

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.

Upvotes: 0

Grace
Grace

Reputation: 888

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), '%')

Upvotes: 1

Kld
Kld

Reputation: 7068

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.

Upvotes: 1

Related Questions