简体   繁体   English

带有多个子字符串的 Mysql CASE

[英]Mysql CASE with multiple substring legths

I have hundreds of phone number of the world.我有数百个世界电话号码。 Each has its country prefix (the prefix varies: some are 1, 2, 3 or 4 digit long) + the phone number.每个都有其国家/地区前缀(前缀各不相同:有些是 1、2、3 或 4 位数字)+ 电话号码。 I want to write a mysql query, which will show me the Country name by using the prefix.我想编写一个 mysql 查询,它将使用前缀向我显示国家/地区名称。

Example : If i use sub-string for the first 3 digits, its working fine.示例:如果我对前 3 位数字使用子字符串,则它工作正常。 But how i can show the prefixes which are 2 or 4 digit long ?但是如何显示 2 位或 4 位长的前缀?

SELECT(
CASE (SUBSTR(Number,1,3))
WHEN '998' Then 'Uzbekistan '
WHEN '996' Then 'Kyrgyzstan '
WHEN '995' Then 'Georgia '
.....
....
ELSE 'OTHERS' END ) AS Country

A simple solution is based on the fact that the CASE statement is evaluated sequentially.一个简单的解决方案是基于CASE语句按顺序计算的事实。

SELECT(
    CASE 
      WHEN SUBSTR(Number,1,2) = '23' Then 'Try For 23 '   
      WHEN SUBSTR(Number,1,3) = '998' Then 'Uzbekistan '
      WHEN SUBSTR(Number,1,3) = '996' Then 'Kyrgyzstan '
      WHEN SUBSTR(Number,1,3) = '995' Then 'Georgia '
     .....
     ....
     ELSE 'OTHERS' END ) AS Country

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

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