基本上,我想从一列中进行选择,直到该字段中有一个数字为止。例如,我的邮政编码数据看起来像
+----------+ | postcode | +----------+ | RG41 5LY | | RG7 6LO | | SW 1AA | | M 3BV | +----------+
我希望能够仅获得第一个字母,例如RG,SW,M。因此,请选择数据,直到到达非字母字符为止。我想这样做,所以我可以按第一个字母字符将其分组
目前,我的查询是这样的:
SELECT COUNT(*) as count, postcodeArea FROM Client c (SELECT UPPER((SUBSTRING_INDEX(postcode, ' ', 1))) AS postcodeArea
它根据列进行分组,直到第一个空格
谢谢!
假设您的邮政编码中最多包含3个前导字母,则可以尝试以下操作:
SELECT COUNT(*) tot, pcode FROM ( SELECT CASE WHEN postcodearea REGEXP '^[A-Z][A-Z][A-Z].*' THEN SUBSTRING(postcodearea,1,3) WHEN postcodearea REGEXP '^[A-Z][A-Z].*' THEN SUBSTRING(postcodearea,1,2) WHEN postcodearea REGEXP '^[A-Z].*' THEN SUBSTRING(postcodearea,1,1) ELSE '' END AS pcode FROM client ) e GROUP BY pcode