Mysql选择查询以匹配zipcodes
表名:联系人Mysql选择查询以匹配zipcodes,mysql,Mysql,表名:联系人 id | postal_code 1 | 41575-8907 2 | 41575 3 | 65789 4 | 396001 5 | 65789-0013 我想要的是检查邮政编码,如果其模式为xxxxx-xxxx,则只取前5位,并丢弃-xxxx 可能正在检查如下正则表达式:[0-9]{5}-[0-9{4}] 我想要一个select mysql查询,显示以下输出 id | postal_code 1 | 41575 2 | 41575 3 | 65789 4 |
id | postal_code
1 | 41575-8907
2 | 41575
3 | 65789
4 | 396001
5 | 65789-0013
我想要的是检查邮政编码,如果其模式为xxxxx-xxxx,则只取前5位,并丢弃-xxxx
可能正在检查如下正则表达式:[0-9]{5}-[0-9{4}]
我想要一个select mysql查询,显示以下输出
id | postal_code
1 | 41575
2 | 41575
3 | 65789
4 | 396001
5 | 65789
这样使用:
SELECT id, SUBSTRING_INDEX(code_postal,'-',1);
FROM contacts
演示:
mysql> select * from contacts;
+----+-------------+
| id | code_postal |
+----+-------------+
| 1 | 12345-456 |
| 2 | 12346 |
| 3 | 12347 |
| 4 | 12348-7865 |
| 5 | 10348-77765 |
+----+-------------+
5 rows in set (0.00 sec)
mysql> select id, substring_index(code_postal,'-',1) from contacts;
+----+------------------------------------+
| id | substring_index(code_postal,'-',1) |
+----+------------------------------------+
| 1 | 12345 |
| 2 | 12346 |
| 3 | 12347 |
| 4 | 12348 |
| 5 | 10348 |
+----+------------------------------------+
5 rows in set (0.00 sec)