Good day. The following task has been added to the script; about 60 variables are transferred according to them; you need to sort them from the table and display all matches. Naturally it is necessary to solve this question in a cycle. But it would not be desirable to write requests in a cycle. Prompt the function as possible to implement the following conditions. We take one variable, select all matches and pull out their identifier. Then we take the second variable, if it exists, then from the already selected fields that were returned by the first query, we perform the sorting by the second variable and so on.

  • LEFT JOIN? - Vfvtnjd
  • LEFT JOIN as far as I know for the implementation of sampling from different tables here is one table. - alexsis20102
  • if you have all the data in one table, and the requested fields are the same in the select query, you can also have a union . select * from table where field1 = $ var1 union select * from table where field2 = $ var2. etc; - Vfvtnjd
  • no a little bit wrong - alexsis20102
  • > LEFT JOIN as far as I know to implement sampling from different tables, here is a single table // One table can join itself. Select mt1 . field1 from my_table as mt1 left join my_table as mt2 on mt1 . field2 = mt2 . field2 - knes

2 answers 2

It is a cycle. It is better to create a request once and get the necessary data than manually filter the data. especially in 60 fields. Imagine that your filter corresponds to one record or none at all, and you will sequentially iterate through 100500 records corresponding only to the first variable, then 100500 for the second variable, etc. and the last variable will say that none of the 100,500 are suitable.

An example of the formation of where clause (literally, this is not necessary, just to clarify the meaning):

 $where = ""; foreach ($arr as $k => $v) { if ($v!="") $where.="AND "+$k+" = '"+$v+"'"; ... 

perform this request once and receive the necessary data immediately.

    Maybe so? Do you form a query dynamically based on variables?

     select * from table where id in (select id from table where field1=$var1) order by field1, field2 ... 

    the only thing that you don’t check is that there are certain fields in one request - you should probably check it all beforehand, and then form a request for the existing fields.