MYSQL—数字格式问题

发布于 2024-11-17 22:09:48 字数 363 浏览 3 评论 0原文

我现在正在做一项工作,客户希望用户能够通过产品代码搜索产品。

产品代码的格式如下:123.4567.89

因此,无论用户输入带句点、不带句点或带空格的数字,搜索框都应返回该产品。

因此,以下所有内容都应返回产品:123.4567.89、123456789、123 4567 89。

我当前的查询如下所示:

SELECT      *
FROM        products
WHERE       product_code LIKE '%$search_code%'"

我不知道如何修改它以包括如何修改的所有不同可能性用户将输入这些数字。

预先感谢您的任何帮助。

I'm working on a gig right now where the client wants the user to be able to search for a product by product code.

A product code is formatted like so: 123.4567.89

So, the search box should return that product whether the user enters the number with the periods, without the periods, or with spaces.

So, all of the following should return the product: 123.4567.89, 123456789, 123 4567 89.

My current query looks like so:

SELECT      *
FROM        products
WHERE       product_code LIKE '%$search_code%'"

I'm at a loss as to how I would revise that to include all the different possibilities of how a user would input these numbers.

Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

山人契 2024-11-24 22:09:49
  1. [前端]限制用户可以输入的字符。只允许使用句点和空格。不允许使用任何字母字符(如果您的所有产品 SKU 都是数字)。

  2. [中间层] 表单发布后,请仔细检查后端数据是否存在无关字符。如果客户端设法通过前端的验证,您可以在后端捕获它。使用简单的搜索并用您选择的语言进行替换。

  3. [数据库/后端]将数据限制为仅限数字并将 SKU 发送到数据库查询后,删除产品表中的所有句点。如果您知道只使用句点来存储 SKU,则只需搜索排除它们即可,例如

<前><代码>选择*
来自产品
WHERE REPLACE(product_code,'.','') = @productCode

避免通配符 %% 搜索,它们的成本很高。

  1. [Front End] Limit the characters the user can enter. Only allow periods and spaces. Don't allow any alpha characters (if all your product SKUs are numerical).

  2. [Middle Tier] After the form is posted, double check the data for extraneous characters on the back end. If somehow the client managed to bipass the validation on the front end, you can catch it on the back end. Use a simple search and replace in your language of choice.

  3. [Database/Back-End] Once the data is restricted to only numeric digits and you send the SKU to your database query, strip out all periods on your products table. If you know you only use periods to store the SKUs, just search excluding them, e.g.

SELECT *
FROM products
WHERE REPLACE(product_code,'.','') = @productCode

Avoid wildcard %% searches, they're expensive.

萌面超妹 2024-11-24 22:09:49

在进行搜索之前,您必须对用户输入的号码进行规范化,即:使其与数据库中存储的号码具有相同的格式。

例如,如果数据库中存储的数字不带句点(如 123456789),则必须预处理用户输入的数字,以删除其中的句点、空格和任何其他字符。

编辑:如果数字与句点一起存储在数据库中,那么您还需要通过删除句点来标准化它们,正如 @HertzaHaeon 在他的答案中指出的那样。

You have to normalize the number input by the user before doing the search, that is: make it have the same format as the number stored in the database.

For instance, if the numbers are stored in the database without the periods (like 123456789), you have to pre-process the number input by the user to also remove the periods, spaces and any other characters from it.

Edit: if the numbers are stored in the database with the periods, than you also need to normalize them by removing the periods as @HertzaHaeon pointed in his answer.

少年亿悲伤 2024-11-24 22:09:49

如何删除数据库代码和 searchg 输入中的点和空格,这样就只剩下数字了?像这样的事情:

WHERE REPLACE(product_code, '.', '') LIKE '%formatted_search_code%'

对于搜索输入,您可以使用正则表达式或简单的子字符串替换来删除除数字之外的所有内容。

How about removing dots and spaces in both the database code and the searcg input, so you have just the digits? Something like this:

WHERE REPLACE(product_code, '.', '') LIKE '%formatted_search_code%'

For the search input, you can strip everything but digits form it with a regular expression or simple substring replacement.

时光与爱终年不遇 2024-11-24 22:09:49

我认为解决您问题的最佳方法是格式化搜索值,使其与数据库中使用的格式匹配。但如果用户填写完整的产品编号,您只会找到该产品。如果这不是所需的解决方案,并且您希望能够让用户填写产品代码的任何部分,并找到具有包含该代码的代码的所有产品,我认为您应该过滤掉数据库中的期间。

最快的解决方案是在数据库中实际执行此操作。从产品代码中删除点,或添加包含不带点的产品代码的额外字段。当数据集变大时,这将加快查询速度。

如果您不想这样做,您可以随时过滤掉搜索查询中的点:

REPLACE(product_code,'.','') LIKE '%$search_code%'

这会起到作用,但当数据集变大时可能会非常慢。

I think the best solution to your problem is to format the search value so that is matches the format used in your database. But than you will only find the product, if the user fills in the whole product number. If this is not the desired solution and you want to be able to let the user fill in any part of a product code and find al the products that have a code containing that that I think you should filter out the periods in your database.

The fasted solution would be to do it actually in your database. Remove the dots from your product code or add an extra field containing the product code without dots. This will speed up the query when the dataset gets larger.

If you not want to do that you can always filter out the dots in the search query:

REPLACE(product_code,'.','') LIKE '%$search_code%'

This will do the thrick but can be very slow when the dataset gets bigger.

疯了 2024-11-24 22:09:49

我一直在处理社会安全号码的问题。我的解决方案是获取输入字符串,去掉非数字字符,确保字符串的长度正确,然后使用 substring 函数将字符串分解,然后将其与分隔符放回一起。如果您使用 PHP,该函数可能如下所示:

<?php
function FormatProductCode($String) {
    $String = preg_replace("/[^0-9]/", "", $String);
    if ($String == "") {
        return null;
    }
    $String = str_pad($String, 9, "0", STR_PAD_LEFT);
    return substr($String, 0, 3) . "." . substr($String, 3, 4) . "." . substr($String, 7, 2);
}
?>

每当您需要将数据输入数据库或比较数据时,请使用此函数。

I work with this all the time with social-security numbers. My solution is to take your input string, strip out and characters that are not digits, make sure that the string is the proper length and then use the substring function to break the string up and then put it back together with the delimiters. If you're using PHP, the function might look like this this:

<?php
function FormatProductCode($String) {
    $String = preg_replace("/[^0-9]/", "", $String);
    if ($String == "") {
        return null;
    }
    $String = str_pad($String, 9, "0", STR_PAD_LEFT);
    return substr($String, 0, 3) . "." . substr($String, 3, 4) . "." . substr($String, 7, 2);
}
?>

Use this function any time that you need to input data into the database or compare data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文