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.
2 answers
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.
mt1
.field1
frommy_table
asmt1
left joinmy_table
asmt2
onmt1
.field2
=mt2
.field2
- knes