我正在尝试将员工数据包括在电话号码为10位的查询中。它们在表格中显示为###.###.####或###.##.####.######。
如何使用where函数来仅包含10位数字。下面的代码和我尝试过的其他代码都会导致错误。
select column_a, column_b
from employees
where phone_number like '__________'发布于 2020-04-07 10:31:51
我将在输入中进行验证
<input runat="server" type="text" class="form-control" maxlength="10" id="txtPhoneNumber" placeholder="### ### ####" />发布于 2020-04-07 10:49:08
如果您的表很小,您可以从电话号码中剥离非号码,然后检查其长度。执行此操作的函数因数据库而异。下面是如何使用regexp_replace在MySQL中实现这一点。
select column_a, column_b
from employees
where length(regexp_replace(phone_number, '[^[:digit:]]', '')) = 10这对于小桌子来说是很好的。如果您的表很大,这将会遇到性能问题。因为它是计算出来的,所以它必须扫描每一行。处理此问题的一种方法是添加一个归一化电话号码的generated column。
alter table employees
add column phone_number_normalized varchar(20)
generated always as (
regexp_replace(phone_number, '[^[:digit:]]', '')
)
stored;
create index phone_number_normalized on employees (phone_number_normalized);这将添加一个从phone_number自动生成的列phone_number_normalized,并且只存储数字。并将其编入索引。这样,您的查询和其他查询就会变得更简单、更高效。
select column_a, column_b
from employees
where length(phone_number_normalized) = 10最后,如果您经常使用电话号码,而不是将它们存储为字符串,您可能希望解析它们并将它们放入它们自己的表中。然后,您可以存储它们的国家代码、区号等内容。
https://stackoverflow.com/questions/61071857
复制相似问题