操纵 select 查询的输出
Manipulate the output of select query
我需要从数据库中检索唯一的手机号码。
问题是数字没有固定格式,有些以
+CountryCode
或 00CountryCode
或没有 +
和 country code
所以我想使用 If 来操纵它们以具有单一格式。
但我不想改变数据库中的价值我只想改变它
在查询本身中。
我不知道如何使用 DISTINCT
这是我目前所做的
SELECT DISTINCT PhoneNumber
FROM User
where (len(PhoneNumber) = 13 or len(PhoneNumber) = 10 or phoneNumber like '311%' or phoneNumber like '00311%' )
SELECT DISTINCT RIGHT(PHONENUMER,10) FROM USER
这有用吗,即获取最后 10 位数字?
使用case when
。这是 MySQL
的解决方案
select distinct case when length(phone) = 10 then concat('00382', right(phone ,(length(phone)-1)))
when length(phone) = 13 then replace(phone, '+', '00')
when length(phone) = 12 then concat ('00', phone)
else phone
end
from myTable;
这里我假设您将有 phone 个长度为 10 位的数字,例如:0123456789
我还假设当数字有国家代码时,第一位数字不显示,例如:00382123456789(14 位长)。
假设我已经这样做了:
如果号码长 10 位 (0123456789) 添加国家代码 (00382) 和
删除第一个数字 (0)
如果号码长13位则将+替换为00
如果号码长12位则加00
这是DEMO
下面是 SQLServer 的解决方案:
select distinct case when len(phone) = 10 then concat('00382', right(phone ,(len(phone)-1)))
when len(phone) = 13 then replace(phone, '+', '00')
when len(phone) = 12 then concat ('00', phone)
else phone
end
from myTable;
我需要从数据库中检索唯一的手机号码。
问题是数字没有固定格式,有些以
+CountryCode
或 00CountryCode
或没有 +
和 country code
所以我想使用 If 来操纵它们以具有单一格式。 但我不想改变数据库中的价值我只想改变它 在查询本身中。
我不知道如何使用 DISTINCT
这是我目前所做的
SELECT DISTINCT PhoneNumber
FROM User
where (len(PhoneNumber) = 13 or len(PhoneNumber) = 10 or phoneNumber like '311%' or phoneNumber like '00311%' )
SELECT DISTINCT RIGHT(PHONENUMER,10) FROM USER
这有用吗,即获取最后 10 位数字?
使用case when
。这是 MySQL
select distinct case when length(phone) = 10 then concat('00382', right(phone ,(length(phone)-1)))
when length(phone) = 13 then replace(phone, '+', '00')
when length(phone) = 12 then concat ('00', phone)
else phone
end
from myTable;
这里我假设您将有 phone 个长度为 10 位的数字,例如:0123456789 我还假设当数字有国家代码时,第一位数字不显示,例如:00382123456789(14 位长)。
假设我已经这样做了:
如果号码长 10 位 (0123456789) 添加国家代码 (00382) 和 删除第一个数字 (0)
如果号码长13位则将+替换为00
如果号码长12位则加00
这是DEMO
下面是 SQLServer 的解决方案:
select distinct case when len(phone) = 10 then concat('00382', right(phone ,(len(phone)-1)))
when len(phone) = 13 then replace(phone, '+', '00')
when len(phone) = 12 then concat ('00', phone)
else phone
end
from myTable;