A
A
Alexander Petrov2019-06-04 10:15:17
MySQL
Alexander Petrov, 2019-06-04 10:15:17

How to make a wildcard query in Rails or SQL?

I have a table in which there is a pattern for searching files, for example:
table.pattern='file_1'
I receive a file file_1_2018-12-08.xlsx
How to make a request so that I give the full file name, and I get all the lines that have is the value "file_1" contained in the file name?
I understand how to do it if the value of the line contained the name of the file, and I find the desired line using the pattern. it's easy. But you have to do the opposite.
PS I have a Rails project. But if you write pure SQL code, then it is also welcome)
PPS How I do it now:
I iterate over all the rows in the table and just compare whether it matches the pattern? It turns out that for one file I need to go through all the lines and make comparisons equal to the number of lines. This is terrible and I want to do it right, but I don’t understand how to execute the request.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SilentFl, 2019-06-13
@SilentFl

the very first thing that came to mind

Table.where(":filename like concat('%%', pattern, '%%')", filename: 'filename4.xls')
# SELECT  "tables".* FROM "tables" WHERE ('filename4.xls' like concat('%%', pattern, '%%'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question