简体   繁体   English

将varchar转换为仅用于搜索查询的int?

[英]Converting varchar into an int just for the search query?

What I've been trying to do is to select a row from a table while treating the varchar cells as int ones, 我一直试图做的是从表中选择一行,同时将varchar单元视为int单元,

Here's a little explanation: I have a table of phone numbers, some have "-" in them, some don't. 这里有个简单的解释:我有一张电话号码表,其中有些带有“-”,有些则没有。

I wanted to select a number from the database, without including those "-" in the query. 我想从数据库中选择一个数字,而在查询中不包括那些“-”。 So I used this preg_replace function: 所以我使用了这个preg_replace函数:

$number = preg_replace("/[^0-9]/","",$number); //that leaves only the numbers in the variable

and then I run the following query: 然后运行以下查询:

"SELECT * FROM `contacts` WHERE `phone` = '{$number}'"

Now, of course it won't match sometimes since the number Im searching may have "-" in the database, so I tried to look for a solution, on solution is just converting the cells into int's, but I'm not interested in doing that, 现在,由于有时Im搜索中的数字可能在数据库中带有“-”,因此有时当然不会匹配,所以我试图寻找一种解决方案,因为解决方案只是将单元格转换为int,但是我对此并不感兴趣。这样做

So after looking around, I found a MySQL function named CAST, used like : CAST(phone AS UNSIGNED) 因此,环顾四周后,我发现了一个名为CAST的MySQL函数,其用法类似于:CAST(phone AS UNSIGNED)

I tried to mess with it, but it didn't seem to work. 我试图弄乱它,但似乎没有用。

Edit: 编辑:

I kept looking around for a solution, and eventually used MySQL's REPLACE function for that. 我一直在寻找解决方案,并最终使用了MySQL的REPLACE函数。

"SELECT * FROM `contacts` WHERE REPLACE(phone,'-','') = '{$number}'"

Thank you all for your help. 谢谢大家的帮助。

1. The easiest way to do it might be by using the REPLACE operator. 1.最简单的方法可能是使用REPLACE运算符。

SELECT * FROM `contacts` WHERE REPLACE(REPLACE(`phone`, '-', ''), ' ', '') = '5550100';

What it does is simpy replacing all whitespaces and dashes with nothing, namely removing all spaces and dashes. 它所做的是简单地用所有内容替换空白和破折号,即删除所有空格和破折号。

2. Another alternative to solve the problem would be to use LIKE . 2.解决该问题的另一种方法是使用LIKE If the phone numbers with a dash always are formatted the same way like 555-0100 and 555-0199 you can simple insert a %-sign instead of the dash. 如果带破折号的电话号码总是像555-0100和555-0199一样格式化,则可以简单地插入%符号而不是破折号。 If your number may be formatted in different ways you can insert a %-between every character. 如果您的数字格式可能不同,则可以在每个字符之间插入%-。 It's not a beautiful solution but it does the trick. 这不是一个漂亮的解决方案,但可以解决问题。

SELECT * FROM `contacts` WHERE `phone` LIKE '555%0100'; 

or 要么

SELECT * FROM `contacts` WHERE `phone` LIKE '5%5%5%0%1%0%0';

3. You can use regular expressions. 3.您可以使用正则表达式。 Since MySQL doesn't implement regex replace functions you need to use user defined functions. 由于MySQL不实现正则表达式替换功能,因此您需要使用用户定义的功能。 Have a look at https://launchpad.net/mysql-udf-regexp . 看看https://launchpad.net/mysql-udf-regexp It supports REGEXP_LIKE, REGEXP_SUBSTR, REGEXP_INSTR and REGEXP_REPLACE. 它支持REGEXP_LIKE,REGEXP_SUBSTR,REGEXP_INSTR和REGEXP_REPLACE。

Edit: Removed my first answer and added some other alternatives. 编辑:删除了我的第一个答案,并添加了其他一些替代方法。

MySQL doesn't support extraction of regex matches. MySQL不支持提取正则表达式匹配项。

You could try writing a stored function to handle it, but your best bet is to convert the data to ints so that all the numbers are uniform . 您可以尝试编写一个存储函数来处理它,但是最好的选择是将数据转换为int,以便所有数字都是统一的 I know you said you don't want to do that, but if you can , then it's the best thing to do . 我知道您说过您不想这样做,但是如果可以的话 ,那是最好的选择 Otherwise, you could do something like: 否则,您可以执行以下操作:

"SELECT * FROM `contacts` WHERE `phone` = '{$number}' OR `phone` = '{$number_with_dashes}'"

That is, search for the plain number OR the number with dashes. 即,搜索纯数字OR带破折号的数字。

我一直在寻找解决方案,并最终使用了MySQL的REPLACE函数。

"SELECT * FROM `contacts` WHERE REPLACE(phone,'-','') = '{$number}'"

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

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