简体   繁体   English

MySQL / PHP-筛选非数字字段上的数字值

[英]MySQL/PHP - Filter on numeric values on fields that are not numeric

Basically, the idea is that people filter search results on the length. 基本上,人们的想法是根据长度过滤搜索结果。 However, length is stored in a text field, as this can be different from item to item. 但是,长度存储在文本字段中,因为每个项目的长度可能不同。 An item could be 16,20 metres, and another could be 8.1m. 一个项目可能是16,20米,另一个可能是8.1m。

How would one even start? 一个人怎么会开始呢? Get all the possible values from the database, change them to the format that is filtered on (parse everything to numeric only, comma separated?), get the associated ID, and then only show all the info related to those IDs, or is there a way I haven't found yet? 从数据库中获取所有可能的值,将其更改为过滤的格式(将所有内容解析为仅数字,用逗号分隔?),获取关联的ID,然后仅显示与这些ID相关的所有信息,或者是否存在我还没有找到一种方法?

Kind regards. 亲切的问候。

edit: Standardizing format is a solution, but not one I can apply in this situation. 编辑:标准化格式是一种解决方案,但在这种情况下我无法采用。 I am not allowed to do this. 我不允许这样做。 It gets worse - the filtering can have both a minimum and a maximum value. 情况变得更糟-过滤可能同时具有最小值和最大值。 Minimum: 4 and maximum: 8 should show everything between those lengths. 最小值:4,最大值:8,应显示这些长度之间的所有值。 6.1m, 7,82 metres, 5. 6.1m,7,82米,5。

Because I couldn't change the way the database was set up (standardize is, keep separate fields for the length itself, a float/double, and a field for the appendix), I've decided to go with this approach. 因为我无法更改数据库的设置方式(标准化是,为长度本身保留单独的字段,为float / double和为附录保留字段),所以我决定采用这种方法。

First get all the possible lengths, then: 首先获取所有可能的长度,然后:

foreach($lengths as $length) {
 $cLengths[$length['item_id']] = preg_replace("/[^0-9,.]/", "", str_replace(',', '.', $length['field_value']));
}

Assuming the page would then be called with &minLength=2&maxLength=10 in the URL: 假设随后将使用URL中的&minLength = 2&maxLength = 10来调用该页面:

$minLength = $_GET['minLength'];
$maxLength = $_GET['maxLength'];
if(!is_null($minLength) && !is_null($maxLength)) {
 $filteredItems = '';
 foreach($cLengths as $itemId => $cL) {
  if($cL >= $minLength && $cL <= $maxLength) {
   $filteredItems .= $itemId . ',';
  }
 }
 $filteredItems = substr($filteredItems, 0, -1);
 $where .= 'item_id IN(' . $filteredItems . ') AND ';
}

I would recommend to standardize a format for length. 我建议标准化长度格式。 It is hard to filter numeric values stored in different formats. 很难过滤以不同格式存储的数值。

You can use the LIKE operator to search for any pattern in a string: 您可以使用LIKE运算符搜索字符串中的任何模式:

SELECT * FROM table WHERE length LIKE '%filtervalue%'

I would simply enforce standard during your query. 我只是在您的查询期间强制执行标准。 Assuming your length column is named "length" and you query table is "table": 假设您的长度列名为“长度”,而查询表为“表”:

SELECT something, replace(length,',','.') + 0 as mylen FROM `table` HAVING mylen BETWEEN 16.2 AND 18

It would not be very fast, you can also use replace + 0 directly in where, it would remove the need for HAVING that is: 速度不是很快,您也可以在其中直接使用replace + 0,这样就不需要HAVING了:

SELECT something FROM `table` replace(length,',','.') + 0 BETWEEN 16.2 AND 18

Good Luck! 祝好运!

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

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