A
A
Anton Boyarshinov2016-11-07 20:17:08
MySQL
Anton Boyarshinov, 2016-11-07 20:17:08

How to organize a search in the database?

It is necessary to organize a search for strings in the MySQL database, but the query will contain only a substring.
That is, there is a database. It contains the id and name fields. Name contains a string of 2-3 words.
And I need to select all rows containing the substring specified in the request, organizing this in java.
Mysql, as far as I know, has built-in support for regular expressions ( RLIKE 'word')
But for some reason it doesn't work like this:

try(PreparedStatement ps = DBConnector.getConnection().prepareStatement("SELECT * FROM items WHERE 'name' RLIKE ?")) {
            ps.setObject(1,searchPattern.getName()); 
        }

How can this problem be solved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RoverWhite, 2016-11-07
@Zhyki

Maybe it's easier like this?
SELECT * FROM items WHERE 'name' LIKE '%?%'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question