1

I have a table as follows:

id    |                query                 -
------------------------------------------------
1     | SELECT COUNT(*) FROM `test`.`user`;
2     |  INSERT INTO `test`.`user` (id,name,email)
         VALUES      ('4','Don','[email protected]');
3     | SELECT COUNT(*) FROM `test`.`data`;

I want to get rows with "test.user" patterns in the query.

0

3 Answers 3

1
SELECT * FROM ... WHERE LOWER(query) LIKE '%`test`.`user`%'

This assumes you don't have any spaces around the .

Sign up to request clarification or add additional context in comments.

Comments

0

You can use LIKE.

SELECT * FROM `tablename` WHERE `query` LIKE '%test%user%'

The % is the wildcard. You can put it at the front, back, or in the middle, and it can be used multiple times. The query above will find anything that has "test" and "user" in that order, with any characters in between, before or after.

1 Comment

Check the use case where it fails: SELECT main.id, usr.name FROM test.main main INNER JOIN db.user usr;
0

You can get more control over pattern matches in your queries if you use REGEXP, e.g.

select `query` from test WHERE `query` REGEXP '.*test.*user.*';

Gives a basic structure for a query using REGEXP, this works for the question you have asked, but can be modified according to your needs.

As shown in this demo sqlfiddle

For a list of available meta character that work with REGEXP see - http://www.tech-recipes.com/rx/484/use-regular-expressions-in-mysql-select-statements/

1 Comment

The above query fails if we have a table name such as "user_duplicate" or "usersomething".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.