Hello. I do not even know how to ask a question. In general, there is a query with a search (like) on the table. With one value, I have no problem. But I can not reach me in any way how to search in this query by several values. Here is what I mean:

There is such a request

SELECT * FROM `table_text` WHERE `table_text`.`text` LIKE :text; 

He is looking for a certain value. But after I try to create a query that will look for several values, though I really have no idea how to implement it. Since I have values ​​from the array, and their number is not always the same. Those. this time the values ​​are 5, in the other there are 2, in the following there may be generally 20, etc. Therefore, I cannot write a query like this:

 SELECT * FROM `table_text` WHERE `table_text`.`text` LIKE :text1 AND `table_text`.`text` LIKE :text2 // и т.д. 

Advise a way out. And I myself can not imagine how to implement it.

  • one
    I think you need to do it by analogy ru.stackoverflow.com/a/600378/191482 .. only limiting on emptiness is not needed ...... and rather not AND needed OR - Alexey Shimansky
  • I would like without crutches. Too not nice work. - dgd hsk
  • ha ......... and you want to make a query in one line with a dynamic number of fields? well good luck - Alexey Shimansky
  • $arrayData = ["yo1", "werui", 'testValue']; $columnName = 'text'; $sql = "SELECT * FROM `table_text` WHERE `$columnName` = '"; $sql .= implode("' AND `$columnName` = '", $arrayData)."'"; echo $sql; - Alexey Shimansky

2 answers 2

I would advise unloading templates into a temporary table. Then the request becomes simple and clear. And you can implement logic quite flexible - and all templates are immediately suitable, and at least one, and at least 2, and strictly 3, and vice versa, no more than 2 fit ...

  • I thought about this option, but immediately threw it away. In sql, is there such a method that would work by listing what should be in a husky? Something like a space. - dgd hsk
  • There are no such "methods". Why threw it away? - Akina

So you can just

 Select * from table_text join table on table_text.text like '%' + table.text2 + '%' 

where table will contain your entire list of values