简体   繁体   English

如何应用具有多个值和条件的高级搜索查询

[英]How to apply advance search query with multiple values and conditions

I have a view where user select fields to make an advance search, the fields are: 我有一个视图,其中用户选择要进行高级搜索的字段,这些字段是:

**name**- **age** - **location** - **isPaid** - **date_subscription**, **date_expiration** 

the user might choose one column or make combinations of multiple columns, I am confused if I should use if statement to detect which columns are selected, and then run the query depends on the selected column, but this way I will need to set all the valid conditions, which mean set the all the valid combination. 用户可能会选择一个列或将多个列组合在一起,如果使用if statement来检测选择了哪些列,然后根据所选择的列运行查询,我会感到困惑,但是这种方式我需要设置所有有效条件,表示设置所有有效组合。 Is there an other way to execute such queries? 还有其他执行此类查询的方法吗?

I was writing this query, but i stopped because i just realize how long it will be: 我正在编写此查询,但是我停了下来,因为我只知道它将持续多长时间:

SELECT * FROM internetclientdetails icd INNER JOIN internetclient ic on ic.id = icd.icid WHERE
                        (icd.date_sub <=".$start_dateSubsc." and cd.date_sub >= ".$end_dateSubsc.")
                        OR
                        (icd.date_exp <=".$start_dateExp." and cd.date_exp >= ".$end_dateExp.")
                        OR
                        (
                            (icd.date_sub <=".$start_dateSubsc." and cd.date_sub >= ".$end_dateSubsc.")
                            AND 
                            (icd.date_exp <=".$start_dateExp." and cd.date_exp >= ".$end_dateExp.")
                        )
                        OR 
                        .
                        .
                        .

but this is too long to be write since i still have 4 left fields to set OR , AND operators 但这太长了,因为我仍然有4个左字段可以设置ORAND运算符

Generally you add some already query building library, which can construct valid SQL from the input you select. 通常,您添加一些已经查询的构建库,该库可以从您选择的输入中构建有效的SQL。

There are a number of them, for example, Doctrine DB abstraction layer , Medoo and a number of others. 其中有很多,例如, Doctrine DB抽象层Medoo等。

For example, in Medoo a rather complex query such as: 例如,在Medoo中,相当复杂的查询如下:

SELECT account,user_name FROM table 
WHERE user_id IN (2,123,234,54) OR 
email IN ('foo@bar.com','cat@dog.com','admin@medoo.in')

will be writen in PHP as: 将用PHP编写为:

$database->select("account", "user_name", [
    "OR" => [
        "user_id" => [2, 123, 234, 54],
        "email" => ["foo@bar.com", "cat@dog.com", "admin@medoo.in"]
    ]
]);

So all you have to do when using medoo is to pass it the correct input from the form. 因此,使用medoo时所要做的就是从表单传递正确的输入。

As regards your question about how the user selects different columns, you can use something like this: 关于用户如何选择不同列的问题,您可以使用类似以下的内容:

$mapping=array("start_dateSubsc"=>"date_sub", "end_dateSubsc"=>"date_sub",...); $ mapping = array(“ start_dateSubsc” =>“ date_sub”,“ end_dateSubsc” =>“ date_sub”,...);

where you list all the possible fields for user to enter in webpage, which are mapped to the real database table column names. 您在其中列出了所有可能供用户在网页中输入的字段,这些字段已映射到实际数据库表的列名。

Then you do something like this, when you process the page: 然后在处理页面时执行以下操作:

$wherequery["OR"]=array();
foreach ($mapping as $userfield => $dbfield)
{
    if array_key_exists($userfield, $_REQUEST)
        array_push($wherequery["OR"], $dbfield => $_REQUEST[$userfield]);
}
$database->select("your columns"),[ $wherequery ]);

This will work for fields that need to be = to what user said and where you must match any of the fields. 这将适用于需要与用户所说的内容以及您必须在何处匹配任何字段的字段。

You would have a bit more to do with fields that can be in range, and process them seperately, as well as processing fields with "AND", but that depends on the range and possibilities of your actual fields. 您可能需要处理更多范围内的字段,然后分别处理它们,以及使用“ AND”处理字段,但这取决于范围和实际字段的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM