I am trying to group rows by a field that either开发者_如何学Go has the pattern [:alpha:][:digit:].*
or [:alpha:][:alpha:][:digit:].*
by the substring up to but excluding the digit. i.e. the returned substring will either have one letter, or two.
I am thinking something along the lines of:
SELECT
LEFT(postcode,IF(ISDIGIT(postcode,2),1,2)) AS area,
COUNT(*) AS num
FROM addresses
GROUP BY
LEFT(postcode,IF(ISDIGIT(postcode,2),1,2))
Except of course there is no ISDIGIT()
function.
I was also thinking of something similar to LEFT(postcode,POSITION_REGEX("\d" IN postcode))
but obviously that doesn't exist either :-/
Database server is running MySQL 4.1.24
Upgrading to 5.0 is possible but would require downtime and hasn't been done yet as it hasn't been necessary so far.SELECT
LEFT(postcode, IF(postcode REGEXP '^.[[:digit:]]', 1, 2)) AS area,
COUNT(*) AS num
FROM addresses
GROUP BY area
精彩评论