从mysql中选择信息

时间:2013-03-28 08:48:33

标签: mysql search

我需要从mysql中选择目的地

数据库包含以下信息

 IBIS, COUNTRY, REGION
 "AFG", "93";
 "AFG Mobile AWCC", "93", "70"
 "AFG Mobile ETSLAT", "93", "78"
 "AFG Mobile AREEBA", "93", "77"
 "AFG Mobile", "93", "75"

我正在寻找信息ZONE COUNTRY +结果应该是IBIS

Search 1 :9378555555 => Mobile AFG ETSLAT
Search 2 :9377755555 => Mobile AFG AREEBA
Search 3 :9399999999 => AFG

感谢您阅读和回复:)

+++++更新

"ALB Fixed ALBTEL";"355";"4249"
"ALB Tirana";"355";"422, 423, 4240, 4241, 4242, 4243, 4244, 4245, 4246, 4247, 4248"

国家/地区var(3)和Region是concat ......

Search 1 :355424955555 => ALB Fixed ALBTEL
Search 2 :35542405555  => ALB Tirana

1 个答案:

答案 0 :(得分:0)

我会用这样的东西:

SELECT
  *
FROM
  yourtable
WHERE
  COUNTRY = LEFT('9399999999', 2)
  AND (REGION = SUBSTRING('9399999999', 3, 2) OR REGION IS NULL)
ORDER BY
  REGION IS NULL
LIMIT 1

请参阅小提琴here

修改

不是最干净的解决方案,但它对我来说没问题:

SET @num = '35542405555';

SELECT *
FROM
  yourtable
WHERE
  @num LIKE CONCAT(COUNTRY, '%')
  AND (REGION IS NULL
       OR FIND_IN_SET(SUBSTRING(@num, LENGTH(COUNTRY)+1, 2), REPLACE(REGION, ' ', ''))
       OR FIND_IN_SET(SUBSTRING(@num, LENGTH(COUNTRY)+1, 3), REPLACE(REGION, ' ', ''))
       OR FIND_IN_SET(SUBSTRING(@num, LENGTH(COUNTRY)+1, 4), REPLACE(REGION, ' ', '')))
ORDER BY
  REGION IS NULL
LIMIT 1

小提琴here。这仅限于2,3或4个字符的区域。