Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql选择查询以匹配zipcodes_Mysql - Fatal编程技术网

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)