简体   繁体   English

如何在MySQL / PHP中按多个字段过滤

[英]How to filter by multiple fields in MySQL/PHP

I'm writing a filter/sorting feature for an application right now that will have text fields above each column. 我现在正在为应用程序编写过滤器/排序功能,该功能将在每列上方显示文本字段。 As the user types in each field, requests will be sent to the back-end for sorting. 当用户在每个字段中键入内容时,请求将被发送到后端进行排序。 Since there are going to be around 6 text fields, I was wondering if there's a better way to sort instead of using if statements to check for each variable, and writing specific queries if say all fields were entered, just one, or just two fields, etc. 由于大约有6个文本字段,所以我想知道是否有更好的排序方式,而不是使用if语句检查每个变量,并写特定的查询(如果说所有字段都输入了,只是一个或两个字段)等

Seems like there would be a lot of if statements. 似乎会有很多if语句。 Is there a more intuitive way of accomplishing this? 有没有更直观的方式来实现这一目标?

Thanks! 谢谢!

Any initial data manipulation, such as sorting, is usually done by the database engine. 任何初始数据操作(例如排序)通常都是由数据库引擎完成的。

Put an ORDER BY clause in there, unless you have a specific reason the sorting needs done in the application itself. 除非您有特殊原因需要在应用程序本身中完成排序,否则在其中放置一个ORDER BY子句。


Edit: You now say that you want to filter the data instead. 编辑:现在您说要过滤数据。 I would still do this at the database level. 我仍然会在数据库级别执行此操作。 There is no sense in sending a huge dataset to PHP, just for PHP to have to wade through it and filter out data there. 向PHP发送巨大的数据集是没有意义的,只是PHP必须经过它并在那里过滤数据。 In most cases, doing this within MySQL will be far more efficient than what you can build in PHP. 在大多数情况下,在MySQL中执行此操作将比在PHP中构建效率高得多。

Since there are going to be around 6 text fields, I was wondering if there's a better way to sort instead of using if statements to check for each variable 由于大约有6个文本字段,我想知道是否有更好的排序方式,而不是使用if语句检查每个变量

Definitely NO. 绝对没有

First, nothing wrong in using several if's in order. 首先,按顺序使用多个if没错。
Trust me - I myself being a huge fan of reducing repetitions of code, but consider these manually written blocks being the best solution. 相信我-我本人是减少代码重复的忠实拥护者,但是请考虑将这些手动编写的块作为最佳解决方案。
Next, although there can be a way to wrap these condition ns some loop, most of time different conditions require different treatment. 接下来,尽管可以通过某种循环包装这些条件,但是大多数情况下,不同的条件需要不同的处理。

however, in your next statements you are wrong: 但是,在下一个语句中,您错了:

and writing specific queries 并编写特定的查询

you need only one query 您只需要一个查询

Seems like there would be a lot of if statements. 似乎会有很多if语句。

why? 为什么? no more than number of fields you have. 不超过您拥有的字段数。

here goes a complete example of custom search query building code: 这是自定义搜索查询构建代码的完整示例:

$w     = array();
$where = '';

if (!empty($_GET['rooms']))     $w[]="rooms='".mesc($_GET['rooms'])."'";
if (!empty($_GET['space']))     $w[]="space='".mesc($_GET['space'])."'";
if (!empty($_GET['max_price'])) $w[]="price < '".mesc($_GET['max_price'])."'";

if (count($w)) $where="WHERE ".implode(' AND ',$w);
$query="select * from table $where"; 

the only fields filled by the user going to the query. 用户进入查询的唯一字段。

the ordering is going to be pretty the same way. 订购将以几乎相同的方式进行。

  • mesc is an abbreviation for the mysql_real_escape_string or any other applicable database-specific string escaping function mesc是mysql_real_escape_string或任何其他适用的数据库特定的字符串转义函数的缩写
select * from Users

order by Creadted desc, Name asc, LastName desc, Status asc

And your records will be sorted by order from query. 您的记录将根据查询的顺序进行排序。

First by Created desc, then by Name asc and so on. 首先按Created desc创建,然后按Name asc依次类推。

But from your question I can see that you are searching for filtering results. 但是从您的问题中我可以看到您正在搜索过滤结果。

So to filter by multiple fileds just append your where, or if you are using any ORM you can do it through object methods. 因此,要按多个文件过滤,只需将您的位置附加在后面,或者如果您使用的是任何ORM,则可以通过对象方法来完成。

But if its simple you can do it this way 但是,如果它很简单,您可以通过这种方式进行

$query = "";
foreach($_POST['grid_fields'] as $key => $value)
{
   if(strlen($query) > 0)
         $query .= ' and '
   $query .= sprintf(" %s LIKE '%s' ", mysql_real_escape_string($key), '%' .mysql_real_escape_string($value) .'%');  
}
if(strlen($query) > 0)
   $original_query .= ' where ' . $query;

this could help you to achieve your result. 这可以帮助您实现目标。

No. You cannot avoid the testing operations when sorting the set, as you have to compare the elements in the set in same way. 不能。在对集合进行排序时,您无法避免测试操作,因为您必须以相同的方式比较集合中的元素。 The vehicle for this is an if statement. 这样做的载体是if语句。

Could you take a look at this? 你能看看这个吗?

WHERE (ifnull(@filter1, 1) = 1 or columnFilter1 = @filter1)
  and (ifnull(@filter2, 1) = 1 or columnFilter2 = @filter2)
  and (ifnull(@filter3, 1) = 1 or columnFilter3 = @filter3)
  and (ifnull(@filter4, 1) = 1 or columnFilter4 = @filter4)
  and (ifnull(@filter5, 1) = 1 or columnFilter5 = @filter5)
  and (ifnull(@filter6, 1) = 1 or columnFilter6 = @filter6)

Please let me know if I'm misunderstanding your question.. It's not like an IF statement batch, and is pretty lengthy, but what do you think? 如果我误解了您的问题,请让我知道。它不像IF语句批处理,而且很冗长,但是您认为呢?

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

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