我正在尝试理解为什么下面的查询会拉出我想不到的邮政编码。
SQL
Select distinct Postcode from tableA where like 'NE1%';显示了2个邮政编码,均以NE1开头
我试过了:
Select distinct Postcode from tableA where rlike '^NE[0-1]%'显示了许多邮政编码,包括上面的2个,例如NE27 0EZ -我假设是因为它在邮政编码的第二部分有一个零,但不知道为什么会出现NE2 2NE!
我的目标是过滤所有以N(不是NE)开头,但只有一个数字作为下一个字符的邮政编码- SQL,而不是python或scala,因为这个过滤器形成了许多邮政编码过滤器中的一个(一个大的or子句)
我本以为所有以N开头的邮政编码,下一个字符是数字,都会起作用:
Select distinct Postcode from tableA where rlike 'N[0-9] %' or 'N[0-9][0-9] %'
select distinct 'rlike' as Func , postcode from npex.npex where postcode rlike '^NE[0-1]*'
union
select distinct 'like', postcode from npex.npex where postcode like 'NE1%'
order by 1;
RESULTS
Func postcode
like NE1 3BB
like NE12 1AB
rlike NE27 0EZ
rlike NE6 2UT
rlike NE27 0LT
rlike NE12 1AB
rlike NE2 2NE
rlike NE3 4DT
rlike NE1 3BB发布于 2021-01-06 16:00:41
不需要*,否则将匹配0个或多个0或1。
select distinct postcode from npex.npex where postcode rlike '^NE[0-1]'如果您希望获得以N开头的数字开头的数字,您可以使用
select distinct postcode from npex.npex where postcode rlike '^N[0-9]'https://stackoverflow.com/questions/65592051
复制相似问题